blob: bcba561f8018cfbc93c7a33d4bf591f3384ca78d [file] [log] [blame]
Damien Zammit43a1f782015-08-19 15:16:59 +10001/*
2 * This file is part of the coreboot project.
3 *
Damien Zammit43a1f782015-08-19 15:16:59 +10004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; version 2 of
8 * the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#define __SIMPLE_DEVICE__
17
Damien Zammit5680faf2016-01-22 22:12:30 +110018#include <cbmem.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100019#include <commonlib/helpers.h>
20#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>
Damien Zammit43a1f782015-08-19 15:16:59 +100023#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>
Damien Zammit43a1f782015-08-19 15:16:59 +100027#include <northbridge/intel/x4x/x4x.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>
Damien Zammit43a1f782015-08-19 15:16:59 +100030
31/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
32u32 decode_igd_memory_size(const u32 gms)
33{
Arthur Heymans27f94ee2016-06-18 21:08:58 +020034 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16,
Damien Zammit43a1f782015-08-19 15:16:59 +100035 32, 48, 64, 128, 256, 96, 160, 224, 352 };
36
Jacob Garberf74f6cb2019-04-08 17:54:35 -060037 if (gms >= ARRAY_SIZE(ggc2uma))
Damien Zammit43a1f782015-08-19 15:16:59 +100038 die("Bad Graphics Mode Select (GMS) setting.\n");
39
40 return ggc2uma[gms] << 10;
41}
42
43/** Decodes used GTT Graphics Memory Size (GGMS) to kilobytes. */
44u32 decode_igd_gtt_size(const u32 gsm)
45{
46 static const u8 ggc2gtt[] = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 2, 3, 4};
47
Jacob Garberf74f6cb2019-04-08 17:54:35 -060048 if (gsm >= ARRAY_SIZE(ggc2gtt))
Damien Zammit43a1f782015-08-19 15:16:59 +100049 die("Bad GTT Graphics Memory Size (GGMS) setting.\n");
50
51 return ggc2gtt[gsm] << 10;
52}
53
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020054/** Decodes used TSEG size to bytes. */
55u32 decode_tseg_size(const u32 esmramc)
56{
57 if (!(esmramc & 1))
58 return 0;
59
60 switch ((esmramc >> 1) & 3) {
61 case 0:
62 return 1 << 20;
63 case 1:
64 return 2 << 20;
65 case 2:
66 return 8 << 20;
67 case 3:
68 default:
69 die("Bad TSEG setting.\n");
70 }
71}
72
Damien Zammit43a1f782015-08-19 15:16:59 +100073u8 decode_pciebar(u32 *const base, u32 *const len)
74{
75 *base = 0;
76 *len = 0;
Arthur Heymans70a1dda2017-03-09 01:58:24 +010077 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Damien Zammit43a1f782015-08-19 15:16:59 +100078 u32 pciexbar = 0;
79 u32 pciexbar_reg;
80 u32 reg32;
81 int max_buses;
82 const struct {
83 u16 num_buses;
84 u32 addr_mask;
85 } busmask[] = {
86 {256, 0xf0000000},
87 {128, 0xf8000000},
88 {64, 0xfc000000},
89 {0, 0},
90 };
91
92 pciexbar_reg = pci_read_config32(dev, D0F0_PCIEXBAR_LO);
93
94 if (!(pciexbar_reg & 1)) {
95 printk(BIOS_WARNING, "WARNING: MMCONF not set\n");
96 return 0;
97 }
98
99 reg32 = (pciexbar_reg >> 1) & 3;
100 pciexbar = pciexbar_reg & busmask[reg32].addr_mask;
101 max_buses = busmask[reg32].num_buses;
102
103 if (!pciexbar) {
104 printk(BIOS_WARNING, "WARNING: pciexbar invalid\n");
105 return 0;
106 }
107
108 *base = pciexbar;
109 *len = max_buses << 20;
110 return 1;
111}
Damien Zammit5680faf2016-01-22 22:12:30 +1100112
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300113static size_t northbridge_get_tseg_size(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200114{
115 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
116 return decode_tseg_size(esmramc);
117}
118
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300119static uintptr_t northbridge_get_tseg_base(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200120{
121 return pci_read_config32(PCI_DEV(0, 0, 0), D0F0_TSEG);
122}
123
124
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300125/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200126 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300127 * CBMEM top downwards to 4 MiB boundary.
128 */
Arthur Heymans340e4b82019-10-23 17:25:58 +0200129void *cbmem_top_chipset(void)
Damien Zammit5680faf2016-01-22 22:12:30 +1100130{
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200131 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300132 return (void *) top_of_ram;
Damien Zammit5680faf2016-01-22 22:12:30 +1100133}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300134
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300135void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300136{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300137 *start = northbridge_get_tseg_base();
138 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300139}
140
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300141void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300142{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300143 uintptr_t top_of_ram;
144
Elyes HAOUASef906092020-02-20 19:41:17 +0100145 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
146 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300147 */
148 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300149 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200150 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300151 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200152 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300153}