blob: 8a69ba80a8619543c452130adbbf3efe72657d37 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Damien Zammit43a1f782015-08-19 15:16:59 +10002
3#define __SIMPLE_DEVICE__
4
Damien Zammit5680faf2016-01-22 22:12:30 +11005#include <cbmem.h>
Damien Zammit43a1f782015-08-19 15:16:59 +10006#include <commonlib/helpers.h>
7#include <stdint.h>
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03008#include <arch/romstage.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100010#include <device/pci_def.h>
11#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030012#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030013#include <cpu/x86/smm.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100014#include <northbridge/intel/x4x/x4x.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030015#include <program_loading.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030016#include <cpu/intel/smm_reloc.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100017
18/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
19u32 decode_igd_memory_size(const u32 gms)
20{
Arthur Heymans27f94ee2016-06-18 21:08:58 +020021 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16,
Damien Zammit43a1f782015-08-19 15:16:59 +100022 32, 48, 64, 128, 256, 96, 160, 224, 352 };
23
Jacob Garberf74f6cb2019-04-08 17:54:35 -060024 if (gms >= ARRAY_SIZE(ggc2uma))
Damien Zammit43a1f782015-08-19 15:16:59 +100025 die("Bad Graphics Mode Select (GMS) setting.\n");
26
27 return ggc2uma[gms] << 10;
28}
29
30/** Decodes used GTT Graphics Memory Size (GGMS) to kilobytes. */
31u32 decode_igd_gtt_size(const u32 gsm)
32{
33 static const u8 ggc2gtt[] = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 2, 3, 4};
34
Jacob Garberf74f6cb2019-04-08 17:54:35 -060035 if (gsm >= ARRAY_SIZE(ggc2gtt))
Damien Zammit43a1f782015-08-19 15:16:59 +100036 die("Bad GTT Graphics Memory Size (GGMS) setting.\n");
37
38 return ggc2gtt[gsm] << 10;
39}
40
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020041/** Decodes used TSEG size to bytes. */
42u32 decode_tseg_size(const u32 esmramc)
43{
44 if (!(esmramc & 1))
45 return 0;
46
47 switch ((esmramc >> 1) & 3) {
48 case 0:
49 return 1 << 20;
50 case 1:
51 return 2 << 20;
52 case 2:
53 return 8 << 20;
54 case 3:
55 default:
56 die("Bad TSEG setting.\n");
57 }
58}
59
Angel Ponsecec9472020-08-03 15:44:27 +020060int decode_pcie_bar(u32 *const base, u32 *const len)
Damien Zammit43a1f782015-08-19 15:16:59 +100061{
62 *base = 0;
63 *len = 0;
Angel Pons8f917b12020-08-03 15:53:20 +020064
Damien Zammit43a1f782015-08-19 15:16:59 +100065 const struct {
66 u16 num_buses;
67 u32 addr_mask;
68 } busmask[] = {
69 {256, 0xf0000000},
70 {128, 0xf8000000},
71 {64, 0xfc000000},
72 {0, 0},
73 };
74
Angel Pons8f917b12020-08-03 15:53:20 +020075 const u32 pciexbar_reg = pci_read_config32(PCI_DEV(0, 0, 0), D0F0_PCIEXBAR_LO);
Damien Zammit43a1f782015-08-19 15:16:59 +100076
77 if (!(pciexbar_reg & 1)) {
78 printk(BIOS_WARNING, "WARNING: MMCONF not set\n");
79 return 0;
80 }
81
Angel Pons8f917b12020-08-03 15:53:20 +020082 const u32 index = (pciexbar_reg >> 1) & 3;
83 const u32 pciexbar = pciexbar_reg & busmask[index].addr_mask;
84 const int max_buses = busmask[index].num_buses;
Damien Zammit43a1f782015-08-19 15:16:59 +100085
86 if (!pciexbar) {
87 printk(BIOS_WARNING, "WARNING: pciexbar invalid\n");
88 return 0;
89 }
90
91 *base = pciexbar;
92 *len = max_buses << 20;
93 return 1;
94}
Damien Zammit5680faf2016-01-22 22:12:30 +110095
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030096static size_t northbridge_get_tseg_size(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020097{
98 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
99 return decode_tseg_size(esmramc);
100}
101
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300102static uintptr_t northbridge_get_tseg_base(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200103{
104 return pci_read_config32(PCI_DEV(0, 0, 0), D0F0_TSEG);
105}
106
107
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300108/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200109 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300110 * CBMEM top downwards to 4 MiB boundary.
111 */
Arthur Heymans340e4b82019-10-23 17:25:58 +0200112void *cbmem_top_chipset(void)
Damien Zammit5680faf2016-01-22 22:12:30 +1100113{
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200114 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300115 return (void *) top_of_ram;
Damien Zammit5680faf2016-01-22 22:12:30 +1100116}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300117
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300118void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300119{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300120 *start = northbridge_get_tseg_base();
121 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300122}
123
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300124void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300125{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300126 uintptr_t top_of_ram;
127
Elyes HAOUASef906092020-02-20 19:41:17 +0100128 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
129 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300130 */
131 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300132 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200133 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300134 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200135 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300136}