blob: 3c068577d017c9ad037230553288f7aceab997cc [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Subrata Banik01ae11b2017-03-04 23:32:41 +05302
Subrata Banik7609c652017-05-19 14:50:09 +05303#include <cbmem.h>
Subrata Banikb6df6b02020-01-03 15:29:02 +05304#include <console/console.h>
Furquan Shaikhcc35f722020-05-12 16:25:31 -07005#include <cpu/cpu.h>
Subrata Banik7609c652017-05-19 14:50:09 +05306#include <device/device.h>
7#include <device/pci.h>
8#include <device/pci_ids.h>
Werner Zehd12530c2018-12-14 13:09:12 +01009#include <intelblocks/acpi.h>
Subrata Banikb6df6b02020-01-03 15:29:02 +053010#include <intelblocks/cfg.h>
Subrata Banik01ae11b2017-03-04 23:32:41 +053011#include <intelblocks/systemagent.h>
Lijian Zhao357e5522019-04-11 13:07:00 -070012#include <smbios.h>
Subrata Banik7609c652017-05-19 14:50:09 +053013#include <soc/iomap.h>
Kyösti Mälkkid6c57142020-12-21 15:17:01 +020014#include <soc/nvs.h>
Subrata Banik01ae11b2017-03-04 23:32:41 +053015#include <soc/pci_devs.h>
Subrata Banik7609c652017-05-19 14:50:09 +053016#include <soc/systemagent.h>
Patrick Rudolph5e007802020-07-27 15:37:43 +020017#include <types.h>
Subrata Banik7609c652017-05-19 14:50:09 +053018#include "systemagent_def.h"
Subrata Banik01ae11b2017-03-04 23:32:41 +053019
Subrata Banik7609c652017-05-19 14:50:09 +053020/* SoC override function */
Aaron Durbin64031672018-04-21 14:45:32 -060021__weak void soc_systemagent_init(struct device *dev)
Subrata Banik01ae11b2017-03-04 23:32:41 +053022{
Subrata Banik7609c652017-05-19 14:50:09 +053023 /* no-op */
Subrata Banik01ae11b2017-03-04 23:32:41 +053024}
25
Aaron Durbin64031672018-04-21 14:45:32 -060026__weak void soc_add_fixed_mmio_resources(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +053027 int *resource_cnt)
28{
29 /* no-op */
30}
31
Aaron Durbin64031672018-04-21 14:45:32 -060032__weak int soc_get_uncore_prmmr_base_and_mask(uint64_t *base,
Pratik Prajapati82cdfa72017-08-28 14:48:55 -070033 uint64_t *mask)
34{
35 /* return failure for this dummy API */
36 return -1;
37}
38
Furquan Shaikh0f007d82020-04-24 06:41:18 -070039__weak unsigned long sa_write_acpi_tables(const struct device *dev,
Werner Zehd12530c2018-12-14 13:09:12 +010040 unsigned long current,
41 struct acpi_rsdp *rsdp)
42{
43 return current;
44}
45
Patrick Rudolphbf72dcb2020-05-12 16:04:47 +020046__weak uint32_t soc_systemagent_max_chan_capacity_mib(u8 capid0_a_ddrsz)
47{
48 return 32768; /* 32 GiB per channel */
49}
50
Angel Pons6724ba42021-01-31 15:06:59 +010051static uint8_t sa_get_ecc_type(const uint32_t capid0_a)
Patrick Rudolph5e007802020-07-27 15:37:43 +020052{
Angel Pons6724ba42021-01-31 15:06:59 +010053 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
Patrick Rudolph5e007802020-07-27 15:37:43 +020054}
55
56static size_t sa_slots_per_channel(const uint32_t capid0_a)
57{
58 return !(capid0_a & CAPID_DDPCD) + 1;
59}
60
61static size_t sa_number_of_channels(const uint32_t capid0_a)
62{
63 return !(capid0_a & CAPID_PDCD) + 1;
64}
65
66static void sa_soc_systemagent_init(struct device *dev)
67{
68 soc_systemagent_init(dev);
69
70 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
71 if (m == NULL)
72 return;
73
74 const uint32_t capid0_a = pci_read_config32(dev, CAPID0_A);
75
Angel Pons6724ba42021-01-31 15:06:59 +010076 m->ecc_type = sa_get_ecc_type(capid0_a);
Patrick Rudolph5e007802020-07-27 15:37:43 +020077 m->max_capacity_mib = soc_systemagent_max_chan_capacity_mib(CAPID_DDRSZ(capid0_a)) *
78 sa_number_of_channels(capid0_a);
79 m->number_of_devices = sa_slots_per_channel(capid0_a) *
80 sa_number_of_channels(capid0_a);
81}
82
Subrata Banik7609c652017-05-19 14:50:09 +053083/*
84 * Add all known fixed MMIO ranges that hang off the host bridge/memory
85 * controller device.
86 */
87void sa_add_fixed_mmio_resources(struct device *dev, int *resource_cnt,
88 const struct sa_mmio_descriptor *sa_fixed_resources, size_t count)
89{
90 int i;
91 int index = *resource_cnt;
92
93 for (i = 0; i < count; i++) {
94 uintptr_t base;
95 size_t size;
96
97 size = sa_fixed_resources[i].size;
98 base = sa_fixed_resources[i].base;
99
100 mmio_resource(dev, index++, base / KiB, size / KiB);
101 }
102
103 *resource_cnt = index;
104}
105
106/*
107 * DRAM memory mapped register
108 *
109 * TOUUD: This 64 bit register defines the Top of Upper Usable DRAM
110 * TOLUD: This 32 bit register defines the Top of Low Usable DRAM
111 * BGSM: This register contains the base address of stolen DRAM memory for GTT
112 * TSEG: This register contains the base address of TSEG DRAM memory
113 */
114static const struct sa_mem_map_descriptor sa_memory_map[MAX_MAP_ENTRIES] = {
115 { TOUUD, true, "TOUUD" },
116 { TOLUD, false, "TOLUD" },
117 { BGSM, false, "BGSM" },
118 { TSEG, false, "TSEG" },
119};
120
121/* Read DRAM memory map register value through PCI configuration space */
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200122static void sa_read_map_entry(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +0530123 const struct sa_mem_map_descriptor *entry, uint64_t *result)
124{
125 uint64_t value = 0;
126
127 if (entry->is_64_bit) {
128 value = pci_read_config32(dev, entry->reg + 4);
129 value <<= 32;
130 }
131
132 value |= pci_read_config32(dev, entry->reg);
133 /* All registers are on a 1MiB granularity. */
134 value = ALIGN_DOWN(value, 1 * MiB);
135
136 *result = value;
137}
138
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700139/* Fill MMIO resource above 4GB into GNVS */
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300140void sa_fill_gnvs(struct global_nvs *gnvs)
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700141{
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700142 struct device *sa_dev = pcidev_path_on_root(SA_DEVFN_ROOT);
143
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700144 sa_read_map_entry(sa_dev, &sa_memory_map[SA_TOUUD_REG], &gnvs->a4gb);
Furquan Shaikhcc35f722020-05-12 16:25:31 -0700145 gnvs->a4gs = POWER_OF_2(cpu_phys_address_size()) - gnvs->a4gb;
146 printk(BIOS_DEBUG, "PCI space above 4GB MMIO is at 0x%llx, len = 0x%llx\n",
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700147 gnvs->a4gb, gnvs->a4gs);
148}
149
Subrata Banik7609c652017-05-19 14:50:09 +0530150static void sa_get_mem_map(struct device *dev, uint64_t *values)
151{
152 int i;
153 for (i = 0; i < MAX_MAP_ENTRIES; i++)
154 sa_read_map_entry(dev, &sa_memory_map[i], &values[i]);
155}
156
157/*
Subrata Banik7609c652017-05-19 14:50:09 +0530158 * These are the host memory ranges that should be added:
159 * - 0 -> 0xa0000: cacheable
160 * - 0xc0000 -> top_of_ram : cacheable
Subrata Banik239272e2020-07-29 11:01:26 +0530161 * - top_of_ram -> TOLUD: not cacheable with standard MTRRs and reserved
Subrata Banik7609c652017-05-19 14:50:09 +0530162 * - 4GiB -> TOUUD: cacheable
163 *
164 * The default SMRAM space is reserved so that the range doesn't
165 * have to be saved during S3 Resume. Once marked reserved the OS
166 * cannot use the memory. This is a bit of an odd place to reserve
167 * the region, but the CPU devices don't have dev_ops->read_resources()
168 * called on them.
169 *
170 * The range 0xa0000 -> 0xc0000 does not have any resources
171 * associated with it to handle legacy VGA memory. If this range
172 * is not omitted the mtrr code will setup the area as cacheable
173 * causing VGA access to not work.
174 *
Subrata Banik239272e2020-07-29 11:01:26 +0530175 * Don't need to mark the entire top_of_ram till TOLUD range (used
176 * for stolen memory like GFX and ME, PTT, DPR, PRMRR, TSEG etc) as
177 * cacheable for OS usage as coreboot already done with mpinit w/ smm
178 * relocation early.
Subrata Banik7609c652017-05-19 14:50:09 +0530179 *
180 * It should be noted that cacheable entry types need to be added in
181 * order. The reason is that the current MTRR code assumes this and
182 * falls over itself if it isn't.
183 *
184 * The resource index starts low and should not meet or exceed
185 * PCI_BASE_ADDRESS_0.
186 */
187static void sa_add_dram_resources(struct device *dev, int *resource_count)
188{
189 uintptr_t base_k, touud_k;
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200190 size_t size_k;
Subrata Banik7609c652017-05-19 14:50:09 +0530191 uint64_t sa_map_values[MAX_MAP_ENTRIES];
192 uintptr_t top_of_ram;
193 int index = *resource_count;
194
Subrata Banik7609c652017-05-19 14:50:09 +0530195 top_of_ram = (uintptr_t)cbmem_top();
196
197 /* 0 - > 0xa0000 */
198 base_k = 0;
199 size_k = (0xa0000 / KiB) - base_k;
200 ram_resource(dev, index++, base_k, size_k);
201
202 /* 0xc0000 -> top_of_ram */
203 base_k = 0xc0000 / KiB;
204 size_k = (top_of_ram / KiB) - base_k;
205 ram_resource(dev, index++, base_k, size_k);
206
207 sa_get_mem_map(dev, &sa_map_values[0]);
208
Subrata Banik239272e2020-07-29 11:01:26 +0530209 /* top_of_ram -> TOLUD */
Subrata Banik7609c652017-05-19 14:50:09 +0530210 base_k = top_of_ram;
Subrata Banik7609c652017-05-19 14:50:09 +0530211 size_k = sa_map_values[SA_TOLUD_REG] - base_k;
212 mmio_resource(dev, index++, base_k / KiB, size_k / KiB);
213
214 /* 4GiB -> TOUUD */
215 base_k = 4 * (GiB / KiB); /* 4GiB */
216 touud_k = sa_map_values[SA_TOUUD_REG] / KiB;
217 size_k = touud_k - base_k;
218 if (touud_k > base_k)
219 ram_resource(dev, index++, base_k, size_k);
220
221 /*
222 * Reserve everything between A segment and 1MB:
223 *
224 * 0xa0000 - 0xbffff: legacy VGA
225 * 0xc0000 - 0xfffff: RAM
226 */
227 mmio_resource(dev, index++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
228 reserved_ram_resource(dev, index++, 0xc0000 / KiB,
229 (1*MiB - 0xc0000) / KiB);
230
231 *resource_count = index;
232}
233
234static bool is_imr_enabled(uint32_t imr_base_reg)
235{
236 return !!(imr_base_reg & (1 << 31));
237}
238
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200239static void imr_resource(struct device *dev, int idx, uint32_t base,
240 uint32_t mask)
Subrata Banik7609c652017-05-19 14:50:09 +0530241{
242 uint32_t base_k, size_k;
243 /* Bits 28:0 encode the base address bits 38:10, hence the KiB unit. */
244 base_k = (base & 0x0fffffff);
245 /* Bits 28:0 encode the AND mask used for comparison, in KiB. */
246 size_k = ((~mask & 0x0fffffff) + 1);
247 /*
248 * IMRs sit in lower DRAM. Mark them cacheable, otherwise we run
249 * out of MTRRs. Memory reserved by IMRs is not usable for host
250 * so mark it reserved.
251 */
252 reserved_ram_resource(dev, idx, base_k, size_k);
253}
254
255/*
256 * Add IMR ranges that hang off the host bridge/memory
Martin Rothf48acbd2020-07-24 12:24:27 -0600257 * controller device in case CONFIG(SA_ENABLE_IMR) is selected by SoC.
Subrata Banik7609c652017-05-19 14:50:09 +0530258 */
259static void sa_add_imr_resources(struct device *dev, int *resource_cnt)
260{
261 size_t i, imr_offset;
262 uint32_t base, mask;
263 int index = *resource_cnt;
264
265 for (i = 0; i < MCH_NUM_IMRS; i++) {
266 imr_offset = i * MCH_IMR_PITCH;
267 base = MCHBAR32(imr_offset + MCH_IMR0_BASE);
268 mask = MCHBAR32(imr_offset + MCH_IMR0_MASK);
269
270 if (is_imr_enabled(base))
271 imr_resource(dev, index++, base, mask);
272 }
273
274 *resource_cnt = index;
275}
276
277static void systemagent_read_resources(struct device *dev)
278{
279 int index = 0;
280
281 /* Read standard PCI resources. */
282 pci_dev_read_resources(dev);
283
284 /* Add all fixed MMIO resources. */
285 soc_add_fixed_mmio_resources(dev, &index);
286 /* Calculate and add DRAM resources. */
287 sa_add_dram_resources(dev, &index);
Julius Wernercd49cce2019-03-05 16:53:33 -0800288 if (CONFIG(SA_ENABLE_IMR))
Subrata Banik7609c652017-05-19 14:50:09 +0530289 /* Add the isolated memory ranges (IMRs). */
290 sa_add_imr_resources(dev, &index);
Furquan Shaikhb53280a2020-11-25 14:30:15 -0800291
292 /* Reserve the window used for extended BIOS decoding. */
293 if (CONFIG(FAST_SPI_SUPPORTS_EXT_BIOS_WINDOW))
294 mmio_resource(dev, index++, CONFIG_EXT_BIOS_WIN_BASE / KiB,
295 CONFIG_EXT_BIOS_WIN_SIZE / KiB);
Subrata Banik7609c652017-05-19 14:50:09 +0530296}
297
298void enable_power_aware_intr(void)
299{
300 uint8_t pair;
301
302 /* Enable Power Aware Interrupt Routing */
303 pair = MCHBAR8(MCH_PAIR);
304 pair &= ~0x7; /* Clear 2:0 */
305 pair |= 0x4; /* Fixed Priority */
306 MCHBAR8(MCH_PAIR) = pair;
307}
308
Tim Wawrzynczakd87af792021-08-24 09:20:14 -0600309void sa_lock_pam(void)
310{
311 const struct device *dev = pcidev_path_on_root(SA_DEVFN_ROOT);
312 if (!dev)
313 return;
314
315 pci_or_config8(dev, PAM0, PAM_LOCK);
316}
317
Subrata Banik7609c652017-05-19 14:50:09 +0530318static struct device_operations systemagent_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100319 .read_resources = systemagent_read_resources,
320 .set_resources = pci_dev_set_resources,
321 .enable_resources = pci_dev_enable_resources,
Patrick Rudolph5e007802020-07-27 15:37:43 +0200322 .init = sa_soc_systemagent_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530323 .ops_pci = &pci_dev_ops_pci,
Julius Wernercd49cce2019-03-05 16:53:33 -0800324#if CONFIG(HAVE_ACPI_TABLES)
Werner Zehd12530c2018-12-14 13:09:12 +0100325 .write_acpi_tables = sa_write_acpi_tables,
326#endif
Subrata Banik7609c652017-05-19 14:50:09 +0530327};
328
329static const unsigned short systemagent_ids[] = {
Felix Singer43b7f412022-03-07 04:34:52 +0100330 PCI_DID_INTEL_GLK_NB,
331 PCI_DID_INTEL_APL_NB,
332 PCI_DID_INTEL_CNL_ID_U,
333 PCI_DID_INTEL_CNL_ID_Y,
334 PCI_DID_INTEL_SKL_ID_U,
335 PCI_DID_INTEL_SKL_ID_Y,
336 PCI_DID_INTEL_SKL_ID_ULX,
337 PCI_DID_INTEL_SKL_ID_H_4,
338 PCI_DID_INTEL_SKL_ID_H_2,
339 PCI_DID_INTEL_SKL_ID_S_2,
340 PCI_DID_INTEL_SKL_ID_S_4,
341 PCI_DID_INTEL_WHL_ID_W_2,
342 PCI_DID_INTEL_WHL_ID_W_4,
343 PCI_DID_INTEL_KBL_ID_S,
344 PCI_DID_INTEL_SKL_ID_H_EM,
345 PCI_DID_INTEL_KBL_ID_U,
346 PCI_DID_INTEL_KBL_ID_Y,
347 PCI_DID_INTEL_KBL_ID_H,
348 PCI_DID_INTEL_KBL_U_R,
349 PCI_DID_INTEL_KBL_ID_DT,
350 PCI_DID_INTEL_KBL_ID_DT_2,
351 PCI_DID_INTEL_CFL_ID_U,
352 PCI_DID_INTEL_CFL_ID_U_2,
353 PCI_DID_INTEL_CFL_ID_H,
354 PCI_DID_INTEL_CFL_ID_H_4,
355 PCI_DID_INTEL_CFL_ID_H_8,
356 PCI_DID_INTEL_CFL_ID_S,
357 PCI_DID_INTEL_CFL_ID_S_DT_2,
358 PCI_DID_INTEL_CFL_ID_S_DT_4,
359 PCI_DID_INTEL_CFL_ID_S_DT_8,
360 PCI_DID_INTEL_CFL_ID_S_WS_4,
361 PCI_DID_INTEL_CFL_ID_S_WS_6,
362 PCI_DID_INTEL_CFL_ID_S_WS_8,
363 PCI_DID_INTEL_CFL_ID_S_S_4,
364 PCI_DID_INTEL_CFL_ID_S_S_6,
365 PCI_DID_INTEL_CFL_ID_S_S_8,
366 PCI_DID_INTEL_ICL_ID_U,
367 PCI_DID_INTEL_ICL_ID_U_2_2,
368 PCI_DID_INTEL_ICL_ID_Y,
369 PCI_DID_INTEL_ICL_ID_Y_2,
370 PCI_DID_INTEL_CML_ULT,
371 PCI_DID_INTEL_CML_ULT_2_2,
372 PCI_DID_INTEL_CML_ULT_6_2,
373 PCI_DID_INTEL_CML_ULX,
374 PCI_DID_INTEL_CML_S,
375 PCI_DID_INTEL_CML_S_G0G1_P0P1_6_2,
376 PCI_DID_INTEL_CML_S_P0P1_8_2,
377 PCI_DID_INTEL_CML_S_P0P1_10_2,
378 PCI_DID_INTEL_CML_S_G0G1_4,
379 PCI_DID_INTEL_CML_S_G0G1_2,
380 PCI_DID_INTEL_CML_H,
381 PCI_DID_INTEL_CML_H_4_2,
382 PCI_DID_INTEL_CML_H_8_2,
383 PCI_DID_INTEL_TGL_ID_U_2_2,
384 PCI_DID_INTEL_TGL_ID_U_4_2,
385 PCI_DID_INTEL_TGL_ID_Y_2_2,
386 PCI_DID_INTEL_TGL_ID_Y_4_2,
387 PCI_DID_INTEL_TGL_ID_H_6_1,
388 PCI_DID_INTEL_TGL_ID_H_8_1,
389 PCI_DID_INTEL_EHL_ID_0,
390 PCI_DID_INTEL_EHL_ID_1,
391 PCI_DID_INTEL_EHL_ID_1A,
392 PCI_DID_INTEL_EHL_ID_2,
393 PCI_DID_INTEL_EHL_ID_2_1,
394 PCI_DID_INTEL_EHL_ID_3,
395 PCI_DID_INTEL_EHL_ID_3A,
396 PCI_DID_INTEL_EHL_ID_4,
397 PCI_DID_INTEL_EHL_ID_5,
398 PCI_DID_INTEL_EHL_ID_6,
399 PCI_DID_INTEL_EHL_ID_7,
400 PCI_DID_INTEL_EHL_ID_8,
401 PCI_DID_INTEL_EHL_ID_9,
402 PCI_DID_INTEL_EHL_ID_10,
403 PCI_DID_INTEL_EHL_ID_11,
404 PCI_DID_INTEL_EHL_ID_12,
405 PCI_DID_INTEL_EHL_ID_13,
406 PCI_DID_INTEL_EHL_ID_14,
407 PCI_DID_INTEL_EHL_ID_15,
408 PCI_DID_INTEL_JSL_ID_1,
409 PCI_DID_INTEL_JSL_ID_2,
410 PCI_DID_INTEL_JSL_ID_3,
411 PCI_DID_INTEL_JSL_ID_4,
412 PCI_DID_INTEL_JSL_ID_5,
413 PCI_DID_INTEL_ADL_S_ID_1,
414 PCI_DID_INTEL_ADL_S_ID_2,
415 PCI_DID_INTEL_ADL_S_ID_3,
416 PCI_DID_INTEL_ADL_S_ID_4,
417 PCI_DID_INTEL_ADL_S_ID_5,
418 PCI_DID_INTEL_ADL_S_ID_6,
419 PCI_DID_INTEL_ADL_S_ID_7,
420 PCI_DID_INTEL_ADL_S_ID_8,
421 PCI_DID_INTEL_ADL_S_ID_9,
422 PCI_DID_INTEL_ADL_S_ID_10,
423 PCI_DID_INTEL_ADL_S_ID_11,
424 PCI_DID_INTEL_ADL_S_ID_12,
425 PCI_DID_INTEL_ADL_S_ID_13,
426 PCI_DID_INTEL_ADL_S_ID_14,
427 PCI_DID_INTEL_ADL_S_ID_15,
428 PCI_DID_INTEL_ADL_P_ID_1,
429 PCI_DID_INTEL_ADL_P_ID_3,
430 PCI_DID_INTEL_ADL_P_ID_4,
431 PCI_DID_INTEL_ADL_P_ID_5,
432 PCI_DID_INTEL_ADL_P_ID_6,
433 PCI_DID_INTEL_ADL_P_ID_7,
434 PCI_DID_INTEL_ADL_P_ID_8,
435 PCI_DID_INTEL_ADL_P_ID_9,
436 PCI_DID_INTEL_ADL_P_ID_10,
437 PCI_DID_INTEL_ADL_M_ID_1,
438 PCI_DID_INTEL_ADL_M_ID_2,
439 PCI_DID_INTEL_ADL_N_ID_1,
440 PCI_DID_INTEL_ADL_N_ID_2,
441 PCI_DID_INTEL_ADL_N_ID_3,
442 PCI_DID_INTEL_ADL_N_ID_4,
Subrata Banik7609c652017-05-19 14:50:09 +0530443 0
444};
445
446static const struct pci_driver systemagent_driver __pci_driver = {
447 .ops = &systemagent_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100448 .vendor = PCI_VID_INTEL,
Subrata Banik7609c652017-05-19 14:50:09 +0530449 .devices = systemagent_ids
450};