Debian Codegraph @

src/northbridge/intel/gm45/memmap.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 // Use simple device model for this file even in ramstage
4 #define __SIMPLE_DEVICE__
5
6 #include <arch/romstage.h>
7 #include <device/pci_ops.h>
8 #include <device/pci_def.h>
9 #include <console/console.h>
10 #include <cpu/x86/mtrr.h>
11 #include <cpu/x86/smm.h>
12 #include <cbmem.h>
13 #include <program_loading.h>
14 #include <cpu/intel/smm_reloc.h>
15 #include <types.h>
16
17 #include "gm45.h"
18
19 /*
20 * Decodes used Graphics Mode Select (GMS) to kilobytes.
21 * The options for 1M, 4M, 8M and 16M preallocated igd memory are
22 * undocumented but are verified to work.
23 */
24 u32 decode_igd_memory_size(const u32 gms)
25 {
26 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64, 128, 256,
27 96, 160, 224, 352 };
28
29 if (gms >= ARRAY_SIZE(ggc2uma))
30 die("Bad Graphics Mode Select (GMS) setting.\n");
31
32 return ggc2uma[gms] << 10;
33 }
34
35 /** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */
36 u32 decode_igd_gtt_size(const u32 gsm)
37 {
38 switch (gsm) {
39 case 0:
40 return 0 << 10;
41 case 1:
42 return 1 << 10;
43 case 3:
44 case 9:
45 return 2 << 10;
46 case 10:
47 return 3 << 10;
48 case 11:
49 return 4 << 10;
50 default:
51 die("Bad Graphics Stolen Memory (GSM) setting.\n");
52 return 0;
53 }
54 }
55
56 /* Decodes TSEG region size to kilobytes. */
57 u32 decode_tseg_size(u8 esmramc)
58 {
59 if (!(esmramc & 1))
60 return 0;
61 switch ((esmramc >> 1) & 3) {
62 case 0:
63 return 1 << 10;
64 case 1:
65 return 2 << 10;
66 case 2:
67 return 8 << 10;
68 case 3:
69 default:
70 die("Bad TSEG setting.\n");
71 }
72 }
73
74 static uintptr_t northbridge_get_tseg_base(void)
75 {
76 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
77
78 u32 tor;
79
80 /* Top of Lower Usable DRAM */
81 tor = (pci_read_config16(dev, D0F0_TOLUD) & 0xfff0) << 16;
82
83 /* Graphics memory comes next */
84 const u32 ggc = pci_read_config16(dev, D0F0_GGC);
85 const u8 esmramc = pci_read_config8(dev, D0F0_ESMRAMC);
86 if (!(ggc & 2)) {
87 /* Graphics memory */
88 tor -= decode_igd_memory_size((ggc >> 4) & 0xf) << 10;
89 /* GTT Graphics Stolen Memory Size (GGMS) */
90 tor -= decode_igd_gtt_size((ggc >> 8) & 0xf) << 10;
91 }
92 /* TSEG size */
93 tor -= decode_tseg_size(esmramc) << 10;
94 return tor;
95 }
96
97 static size_t northbridge_get_tseg_size(void)
98 {
99 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
100 return decode_tseg_size(esmramc) << 10;
101 }
102
103 /* Depending of UMA and TSEG configuration, TSEG might start at any
104 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
105 * CBMEM top downwards to 4 MiB boundary.
106 */
107 uintptr_t cbmem_top_chipset(void)
108 {
109 return ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
110 }
111
112 void smm_region(uintptr_t *start, size_t *size)
113 {
114 *start = northbridge_get_tseg_base();
115 *size = northbridge_get_tseg_size();
116 }
117
118 void fill_postcar_frame(struct postcar_frame *pcf)
119 {
120 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
121 * RAM to cover both cbmem as the TSEG region.
122 */
123 const uintptr_t top_of_ram = cbmem_top();
124 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
125 MTRR_TYPE_WRBACK);
126 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
127 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
128 }