blob: c6fa0e065ca706331b3f275a9ff0a345275da962 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi2efc8802012-11-06 11:03:53 +01002
Arthur Heymans022d2352022-05-06 12:10:39 +02003#include <acpi/acpi.h>
4#include <acpi/acpigen.h>
5#include <boot/tables.h>
Arthur Heymans17ad4592018-08-06 15:35:28 +02006#include <cbmem.h>
Angel Ponsb9bbed22020-08-03 15:11:55 +02007#include <commonlib/helpers.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +01008#include <console/console.h>
Arthur Heymans022d2352022-05-06 12:10:39 +02009#include <cpu/cpu.h>
Arthur Heymans98c92572022-11-07 11:39:58 +010010#include <cpu/intel/speedstep.h>
Arthur Heymans022d2352022-05-06 12:10:39 +020011#include <cpu/intel/smm_reloc.h>
12#include <device/device.h>
Elyes HAOUAS748caed2019-12-19 17:02:08 +010013#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020014#include <device/pci_ops.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010015#include <stdint.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010016
Patrick Georgi2efc8802012-11-06 11:03:53 +010017#include "chip.h"
18#include "gm45.h"
19
Arthur Heymans022d2352022-05-06 12:10:39 +020020static uint64_t get_touud(void)
21{
22 uint64_t touud = pci_read_config16(__pci_0_00_0, D0F0_TOUUD);
23 touud <<= 20;
24 return touud;
25}
26
Elyes HAOUAS6dcdaaf2018-02-09 07:44:31 +010027static void mch_domain_read_resources(struct device *dev)
Patrick Georgi2efc8802012-11-06 11:03:53 +010028{
29 u64 tom, touud;
Arthur Heymans17ad4592018-08-06 15:35:28 +020030 u32 tomk, tolud, uma_sizek = 0, delta_cbmem;
Kyösti Mälkkic1d4d0b2021-06-26 19:09:05 +030031 int idx = 3;
Patrick Georgi2efc8802012-11-06 11:03:53 +010032
33 /* Total Memory 2GB example:
34 *
35 * 00000000 0000MB-2014MB 2014MB RAM (writeback)
36 * 7de00000 2014MB-2016MB 2MB GFX GTT (uncached)
37 * 7e000000 2016MB-2048MB 32MB GFX UMA (uncached)
38 * 80000000 2048MB TOLUD
39 * 80000000 2048MB TOM
40 *
41 * Total Memory 4GB example:
42 *
43 * 00000000 0000MB-3038MB 3038MB RAM (writeback)
44 * bde00000 3038MB-3040MB 2MB GFX GTT (uncached)
45 * be000000 3040MB-3072MB 32MB GFX UMA (uncached)
46 * be000000 3072MB TOLUD
47 * 100000000 4096MB TOM
48 * 100000000 4096MB-5120MB 1024MB RAM (writeback)
49 * 140000000 5120MB TOUUD
50 */
51
52 pci_domain_read_resources(dev);
53
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030054 struct device *mch = pcidev_on_root(0, 0);
Arthur Heymans89089312018-06-26 21:01:40 +020055
Patrick Georgi2efc8802012-11-06 11:03:53 +010056 /* Top of Upper Usable DRAM, including remap */
Arthur Heymans022d2352022-05-06 12:10:39 +020057 touud = get_touud();
Patrick Georgi2efc8802012-11-06 11:03:53 +010058
59 /* Top of Lower Usable DRAM */
Arthur Heymans89089312018-06-26 21:01:40 +020060 tolud = pci_read_config16(mch, D0F0_TOLUD) & 0xfff0;
Patrick Georgi2efc8802012-11-06 11:03:53 +010061 tolud <<= 16;
62
63 /* Top of Memory - does not account for any UMA */
Arthur Heymans89089312018-06-26 21:01:40 +020064 tom = pci_read_config16(mch, D0F0_TOM) & 0x1ff;
Patrick Georgi2efc8802012-11-06 11:03:53 +010065 tom <<= 27;
66
67 printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n",
68 touud, tolud, tom);
69
70 tomk = tolud >> 10;
71
72 /* Graphics memory comes next */
Arthur Heymans89089312018-06-26 21:01:40 +020073 const u16 ggc = pci_read_config16(mch, D0F0_GGC);
Patrick Georgi2efc8802012-11-06 11:03:53 +010074 if (!(ggc & 2)) {
75 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
76
77 /* Graphics memory */
78 const u32 gms_sizek = decode_igd_memory_size((ggc >> 4) & 0xf);
Arthur Heymans8b766052018-01-24 23:25:13 +010079 printk(BIOS_DEBUG, "%uM UMA, ", gms_sizek >> 10);
Patrick Georgi2efc8802012-11-06 11:03:53 +010080 tomk -= gms_sizek;
81
82 /* GTT Graphics Stolen Memory Size (GGMS) */
83 const u32 gsm_sizek = decode_igd_gtt_size((ggc >> 8) & 0xf);
Arthur Heymans8b766052018-01-24 23:25:13 +010084 printk(BIOS_DEBUG, "%uM GTT", gsm_sizek >> 10);
Patrick Georgi2efc8802012-11-06 11:03:53 +010085 tomk -= gsm_sizek;
86
87 uma_sizek = gms_sizek + gsm_sizek;
88 }
Arthur Heymans89089312018-06-26 21:01:40 +020089 const u8 esmramc = pci_read_config8(mch, D0F0_ESMRAMC);
Arthur Heymans8b766052018-01-24 23:25:13 +010090 const u32 tseg_sizek = decode_tseg_size(esmramc);
91 printk(BIOS_DEBUG, " and %uM TSEG\n", tseg_sizek >> 10);
92 tomk -= tseg_sizek;
93 uma_sizek += tseg_sizek;
Patrick Georgi2efc8802012-11-06 11:03:53 +010094
Arthur Heymans17ad4592018-08-06 15:35:28 +020095 /* cbmem_top can be shifted downwards due to alignment.
96 Mark the region between cbmem_top and tomk as unusable */
Arthur Heymans98435ed2022-05-06 12:22:32 +020097 delta_cbmem = tomk - ((uintptr_t)cbmem_top() >> 10);
Arthur Heymans17ad4592018-08-06 15:35:28 +020098 tomk -= delta_cbmem;
99 uma_sizek += delta_cbmem;
100
101 printk(BIOS_DEBUG, "Unused RAM between cbmem_top and TOM: 0x%xK\n",
102 delta_cbmem);
103
Nico Huberca3e1212017-10-02 20:07:53 +0200104 printk(BIOS_INFO, "Available memory below 4GB: %uM\n", tomk >> 10);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100105
Nico Huber58ba83f2021-01-17 21:50:55 +0100106 /* Report lowest memory region */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300107 ram_resource_kb(dev, idx++, 0, 0xa0000 / KiB);
Nico Huber58ba83f2021-01-17 21:50:55 +0100108
109 /*
110 * Reserve everything between A segment and 1MB:
111 *
112 * 0xa0000 - 0xbffff: Legacy VGA
113 * 0xc0000 - 0xfffff: RAM
114 */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300115 mmio_resource_kb(dev, idx++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
116 reserved_ram_resource_kb(dev, idx++, 0xc0000 / KiB, (1*MiB - 0xc0000) / KiB);
Nico Huber58ba83f2021-01-17 21:50:55 +0100117
118 /* Report < 4GB memory */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300119 ram_resource_kb(dev, idx++, 1*MiB / KiB, tomk - 1*MiB / KiB);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100120
121 /*
122 * If >= 4GB installed then memory from TOLUD to 4GB
123 * is remapped above TOM, TOUUD will account for both
124 */
Kyösti Mälkki0a18d642021-06-28 21:43:31 +0300125 upper_ram_end(dev, idx++, touud);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100126
127 printk(BIOS_DEBUG, "Adding UMA memory area base=0x%llx "
128 "size=0x%llx\n", ((u64)tomk) << 10, ((u64)uma_sizek) << 10);
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300129 /* Don't use uma_resource_kb() as our UMA touches the PCI hole. */
130 fixed_mem_resource_kb(dev, idx++, tomk, uma_sizek, IORESOURCE_RESERVE);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100131
Kyösti Mälkkic1d4d0b2021-06-26 19:09:05 +0300132 mmconf_resource(dev, idx++);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100133}
134
Elyes HAOUAS6dcdaaf2018-02-09 07:44:31 +0100135static void mch_domain_set_resources(struct device *dev)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100136{
137 struct resource *resource;
138 int i;
139
Nico Huber58ba83f2021-01-17 21:50:55 +0100140 for (i = 3; i <= 9; ++i) {
Patrick Georgi2efc8802012-11-06 11:03:53 +0100141 /* Report read resources. */
Vladimir Serbinenko40412c62014-11-12 00:09:20 +0100142 resource = probe_resource(dev, i);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100143 if (resource)
144 report_resource_stored(dev, resource, "");
145 }
146
147 assign_resources(dev->link_list);
148}
149
Elyes HAOUAS6dcdaaf2018-02-09 07:44:31 +0100150static void mch_domain_init(struct device *dev)
Patrick Georgi2efc8802012-11-06 11:03:53 +0100151{
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300152 struct device *mch = pcidev_on_root(0, 0);
Arthur Heymans89089312018-06-26 21:01:40 +0200153
Patrick Georgi2efc8802012-11-06 11:03:53 +0100154 /* Enable SERR */
Elyes HAOUAS5ac723e2020-04-29 09:09:12 +0200155 pci_or_config16(mch, PCI_COMMAND, PCI_COMMAND_SERR);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100156}
157
Arthur Heymanse798e6a2017-12-23 23:09:54 +0100158static const char *northbridge_acpi_name(const struct device *dev)
159{
160 if (dev->path.type == DEVICE_PATH_DOMAIN)
161 return "PCI0";
162
Fabio Aiuto61ed4ef2022-09-30 14:55:53 +0200163 if (!is_pci_dev_on_bus(dev, 0))
Arthur Heymanse798e6a2017-12-23 23:09:54 +0100164 return NULL;
165
166 switch (dev->path.pci.devfn) {
167 case PCI_DEVFN(0, 0):
168 return "MCHC";
169 }
170
171 return NULL;
172}
173
Arthur Heymansaade90e2018-01-25 00:33:45 +0100174void northbridge_write_smram(u8 smram)
175{
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300176 struct device *dev = pcidev_on_root(0, 0);
Arthur Heymans48fa9222018-11-19 13:08:01 +0100177
Elyes Haouas5e6b0f02022-09-13 09:55:49 +0200178 if (!dev)
Arthur Heymans48fa9222018-11-19 13:08:01 +0100179 die("could not find pci 00:00.0!\n");
180
181 pci_write_config8(dev, D0F0_SMRAM, smram);
Arthur Heymansaade90e2018-01-25 00:33:45 +0100182}
183
Arthur Heymans022d2352022-05-06 12:10:39 +0200184static void set_above_4g_pci(const struct device *dev)
185{
186 const uint64_t touud = get_touud();
187 const uint64_t len = POWER_OF_2(cpu_phys_address_size()) - touud;
188
189 const char *scope = acpi_device_path(dev);
190 acpigen_write_scope(scope);
191 acpigen_write_name_qword("A4GB", touud);
192 acpigen_write_name_qword("A4GS", len);
193 acpigen_pop_len();
194
195 printk(BIOS_DEBUG, "PCI space above 4GB MMIO is at 0x%llx, len = 0x%llx\n", touud, len);
196}
197
198static void pci_domain_ssdt(const struct device *dev)
199{
200 generate_cpu_entries(dev);
201 set_above_4g_pci(dev);
202}
203
Arthur Heymans2fb6f682022-11-07 09:45:19 +0100204struct device_operations gm45_pci_domain_ops = {
Patrick Georgi2efc8802012-11-06 11:03:53 +0100205 .read_resources = mch_domain_read_resources,
206 .set_resources = mch_domain_set_resources,
Patrick Georgi2efc8802012-11-06 11:03:53 +0100207 .init = mch_domain_init,
208 .scan_bus = pci_domain_scan_bus,
Vladimir Serbinenko33769a52014-08-30 22:39:20 +0200209 .write_acpi_tables = northbridge_write_acpi_tables,
Arthur Heymans022d2352022-05-06 12:10:39 +0200210 .acpi_fill_ssdt = pci_domain_ssdt,
Arthur Heymanse798e6a2017-12-23 23:09:54 +0100211 .acpi_name = northbridge_acpi_name,
Patrick Georgi2efc8802012-11-06 11:03:53 +0100212};
213
Arthur Heymans2fb6f682022-11-07 09:45:19 +0100214struct device_operations gm45_cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200215 .read_resources = noop_read_resources,
216 .set_resources = noop_set_resources,
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300217 .init = mp_cpu_bus_init,
Patrick Georgi2efc8802012-11-06 11:03:53 +0100218};
219
Patrick Georgi2efc8802012-11-06 11:03:53 +0100220static void gm45_init(void *const chip_info)
221{
222 int dev, fn, bit_base;
223
Kyösti Mälkki98a91742018-05-21 21:29:16 +0300224 struct device *const d0f0 = pcidev_on_root(0x0, 0);
Patrick Georgi2efc8802012-11-06 11:03:53 +0100225
226 /* Hide internal functions based on devicetree info. */
227 for (dev = 3; dev > 0; --dev) {
228 switch (dev) {
229 case 3: /* ME */
230 fn = 3;
231 bit_base = 6;
232 break;
233 case 2: /* IGD */
234 fn = 1;
235 bit_base = 3;
236 break;
237 case 1: /* PEG */
238 fn = 0;
239 bit_base = 1;
240 break;
241 }
242 for (; fn >= 0; --fn) {
Angel Ponsb0535832020-06-08 11:46:58 +0200243 const struct device *const d = pcidev_on_root(dev, fn);
244 if (!d || d->enabled)
245 continue;
246 /* FIXME: Using bitwise ops changes the binary */
Patrick Georgi2efc8802012-11-06 11:03:53 +0100247 pci_write_config32(d0f0, D0F0_DEVEN,
Angel Ponsb0535832020-06-08 11:46:58 +0200248 pci_read_config32(d0f0, D0F0_DEVEN) & ~(1 << (bit_base + fn)));
Patrick Georgi2efc8802012-11-06 11:03:53 +0100249 }
250 }
251
252 const u32 deven = pci_read_config32(d0f0, D0F0_DEVEN);
253 if (!(deven & (0xf << 6)))
254 pci_write_config32(d0f0, D0F0_DEVEN, deven & ~(1 << 14));
255}
256
257struct chip_operations northbridge_intel_gm45_ops = {
258 CHIP_NAME("Intel GM45 Northbridge")
Patrick Georgi2efc8802012-11-06 11:03:53 +0100259 .init = gm45_init,
260};
Arthur Heymans98c92572022-11-07 11:39:58 +0100261
262bool northbridge_support_slfm(void)
263{
264 struct device *gmch = __pci_0_00_0;
265 struct northbridge_intel_gm45_config *config = gmch->chip_info;
266 return config->slfm == 1;
267}