blob: 1f1c13f092c1f75bf3ce5b5fc9bb1a32545e7995 [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>
Damien Zammit43a1f782015-08-19 15:16:59 +100031
32/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
33u32 decode_igd_memory_size(const u32 gms)
34{
Arthur Heymans27f94ee2016-06-18 21:08:58 +020035 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16,
Damien Zammit43a1f782015-08-19 15:16:59 +100036 32, 48, 64, 128, 256, 96, 160, 224, 352 };
37
38 if (gms > ARRAY_SIZE(ggc2uma))
39 die("Bad Graphics Mode Select (GMS) setting.\n");
40
41 return ggc2uma[gms] << 10;
42}
43
44/** Decodes used GTT Graphics Memory Size (GGMS) to kilobytes. */
45u32 decode_igd_gtt_size(const u32 gsm)
46{
47 static const u8 ggc2gtt[] = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 2, 3, 4};
48
49 if (gsm > ARRAY_SIZE(ggc2gtt))
50 die("Bad GTT Graphics Memory Size (GGMS) setting.\n");
51
52 return ggc2gtt[gsm] << 10;
53}
54
55u8 decode_pciebar(u32 *const base, u32 *const len)
56{
57 *base = 0;
58 *len = 0;
Arthur Heymans70a1dda2017-03-09 01:58:24 +010059 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Damien Zammit43a1f782015-08-19 15:16:59 +100060 u32 pciexbar = 0;
61 u32 pciexbar_reg;
62 u32 reg32;
63 int max_buses;
64 const struct {
65 u16 num_buses;
66 u32 addr_mask;
67 } busmask[] = {
68 {256, 0xf0000000},
69 {128, 0xf8000000},
70 {64, 0xfc000000},
71 {0, 0},
72 };
73
74 pciexbar_reg = pci_read_config32(dev, D0F0_PCIEXBAR_LO);
75
76 if (!(pciexbar_reg & 1)) {
77 printk(BIOS_WARNING, "WARNING: MMCONF not set\n");
78 return 0;
79 }
80
81 reg32 = (pciexbar_reg >> 1) & 3;
82 pciexbar = pciexbar_reg & busmask[reg32].addr_mask;
83 max_buses = busmask[reg32].num_buses;
84
85 if (!pciexbar) {
86 printk(BIOS_WARNING, "WARNING: pciexbar invalid\n");
87 return 0;
88 }
89
90 *base = pciexbar;
91 *len = max_buses << 20;
92 return 1;
93}
Damien Zammit5680faf2016-01-22 22:12:30 +110094
Kyösti Mälkki811932a2016-07-22 22:53:19 +030095/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020096 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030097 * CBMEM top downwards to 4 MiB boundary.
98 */
Damien Zammit5680faf2016-01-22 22:12:30 +110099void *cbmem_top(void)
100{
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100101 uintptr_t top_of_ram = pci_read_config32(PCI_DEV(0, 0, 0), D0F0_TSEG);
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300102 top_of_ram = ALIGN_DOWN(top_of_ram, 4*MiB);
103 return (void *) top_of_ram;
Damien Zammit5680faf2016-01-22 22:12:30 +1100104}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300105
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300106#define ROMSTAGE_RAM_STACK_SIZE 0x5000
107
Arthur Heymans4ff675e2018-06-03 10:49:11 +0200108/* platform_enter_postcar() determines the stack to use after
109 * cache-as-ram is torn down as well as the MTRR settings to use,
110 * and continues execution in postcar stage. */
111void platform_enter_postcar(void)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300112{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300113 struct postcar_frame pcf;
114 uintptr_t top_of_ram;
115
116 if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE))
117 die("Unable to initialize postcar frame.\n");
118
119 /* Cache the ROM as WP just below 4GiB. */
Nico Huber089b9082018-05-27 14:37:32 +0200120 postcar_frame_add_romcache(&pcf, MTRR_TYPE_WRPROT);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300121
122 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
123 postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
124
125 /* Cache two separate 4 MiB regions below the top of ram, this
126 * satisfies MTRR alignment requirements. If you modify this to
127 * cover TSEG, make sure UMA region is not set with WRBACK as it
128 * causes hard-to-recover boot failures.
129 */
130 top_of_ram = (uintptr_t)cbmem_top();
131 postcar_frame_add_mtrr(&pcf, top_of_ram - 4*MiB, 4*MiB, MTRR_TYPE_WRBACK);
132 postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 4*MiB, MTRR_TYPE_WRBACK);
133
Arthur Heymans4ff675e2018-06-03 10:49:11 +0200134 run_postcar_phase(&pcf);
135
136 /* We do not return here. */
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300137}