blob: 6c0d5f59a6d344ab06a57b5b10958d2c18ad139f [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Subrata Banik01ae11b2017-03-04 23:32:41 +05303
Subrata Banik7609c652017-05-19 14:50:09 +05304#include <cbmem.h>
Subrata Banikb6df6b02020-01-03 15:29:02 +05305#include <console/console.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>
Subrata Banik01ae11b2017-03-04 23:32:41 +053014#include <soc/pci_devs.h>
Subrata Banik7609c652017-05-19 14:50:09 +053015#include <soc/systemagent.h>
16#include "systemagent_def.h"
Subrata Banik01ae11b2017-03-04 23:32:41 +053017
Subrata Banik7609c652017-05-19 14:50:09 +053018/* SoC override function */
Aaron Durbin64031672018-04-21 14:45:32 -060019__weak void soc_systemagent_init(struct device *dev)
Subrata Banik01ae11b2017-03-04 23:32:41 +053020{
Subrata Banik7609c652017-05-19 14:50:09 +053021 /* no-op */
Subrata Banik01ae11b2017-03-04 23:32:41 +053022}
23
Aaron Durbin64031672018-04-21 14:45:32 -060024__weak void soc_add_fixed_mmio_resources(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +053025 int *resource_cnt)
26{
27 /* no-op */
28}
29
Aaron Durbin64031672018-04-21 14:45:32 -060030__weak int soc_get_uncore_prmmr_base_and_mask(uint64_t *base,
Pratik Prajapati82cdfa72017-08-28 14:48:55 -070031 uint64_t *mask)
32{
33 /* return failure for this dummy API */
34 return -1;
35}
36
Werner Zehd12530c2018-12-14 13:09:12 +010037__weak unsigned long sa_write_acpi_tables(struct device *dev,
38 unsigned long current,
39 struct acpi_rsdp *rsdp)
40{
41 return current;
42}
43
Subrata Banik7609c652017-05-19 14:50:09 +053044/*
Subrata Banikb6df6b02020-01-03 15:29:02 +053045 * This function will get above 4GB mmio enable config specific to soc.
46 *
47 * Return values:
48 * 0 = Above 4GB memory is not enable
49 * 1 = Above 4GB memory is enable
50 */
51static int get_enable_above_4GB_mmio(void)
52{
53 const struct soc_intel_common_config *common_config;
54 common_config = chip_get_common_soc_structure();
55
56 return common_config->enable_above_4GB_mmio;
57}
58
59/* Fill MMIO resource above 4GB into GNVS */
60void sa_fill_gnvs(global_nvs_t *gnvs)
61{
62 if (get_enable_above_4GB_mmio()) {
63 gnvs->e4gm = 1;
64 gnvs->a4gb = ABOVE_4GB_MEM_BASE_ADDRESS;
65 gnvs->a4gs = ABOVE_4GB_MEM_BASE_SIZE;
66 printk(BIOS_DEBUG,
67 "PCI space above 4GB MMIO is from 0x%llx to len = 0x%llx\n",
68 gnvs->a4gb, gnvs->a4gs);
69 }
70}
71
72/*
Subrata Banik7609c652017-05-19 14:50:09 +053073 * Add all known fixed MMIO ranges that hang off the host bridge/memory
74 * controller device.
75 */
76void sa_add_fixed_mmio_resources(struct device *dev, int *resource_cnt,
77 const struct sa_mmio_descriptor *sa_fixed_resources, size_t count)
78{
79 int i;
80 int index = *resource_cnt;
81
82 for (i = 0; i < count; i++) {
83 uintptr_t base;
84 size_t size;
85
86 size = sa_fixed_resources[i].size;
87 base = sa_fixed_resources[i].base;
88
89 mmio_resource(dev, index++, base / KiB, size / KiB);
90 }
91
92 *resource_cnt = index;
93}
94
95/*
96 * DRAM memory mapped register
97 *
98 * TOUUD: This 64 bit register defines the Top of Upper Usable DRAM
99 * TOLUD: This 32 bit register defines the Top of Low Usable DRAM
100 * BGSM: This register contains the base address of stolen DRAM memory for GTT
101 * TSEG: This register contains the base address of TSEG DRAM memory
102 */
103static const struct sa_mem_map_descriptor sa_memory_map[MAX_MAP_ENTRIES] = {
104 { TOUUD, true, "TOUUD" },
105 { TOLUD, false, "TOLUD" },
106 { BGSM, false, "BGSM" },
107 { TSEG, false, "TSEG" },
108};
109
110/* Read DRAM memory map register value through PCI configuration space */
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200111static void sa_read_map_entry(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +0530112 const struct sa_mem_map_descriptor *entry, uint64_t *result)
113{
114 uint64_t value = 0;
115
116 if (entry->is_64_bit) {
117 value = pci_read_config32(dev, entry->reg + 4);
118 value <<= 32;
119 }
120
121 value |= pci_read_config32(dev, entry->reg);
122 /* All registers are on a 1MiB granularity. */
123 value = ALIGN_DOWN(value, 1 * MiB);
124
125 *result = value;
126}
127
128static void sa_get_mem_map(struct device *dev, uint64_t *values)
129{
130 int i;
131 for (i = 0; i < MAX_MAP_ENTRIES; i++)
132 sa_read_map_entry(dev, &sa_memory_map[i], &values[i]);
133}
134
135/*
Subrata Banik7609c652017-05-19 14:50:09 +0530136 * These are the host memory ranges that should be added:
137 * - 0 -> 0xa0000: cacheable
138 * - 0xc0000 -> top_of_ram : cacheable
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200139 * - top_of_ram -> BGSM: cacheable with standard MTRRs and reserved
Subrata Banik7609c652017-05-19 14:50:09 +0530140 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
141 * - 4GiB -> TOUUD: cacheable
142 *
143 * The default SMRAM space is reserved so that the range doesn't
144 * have to be saved during S3 Resume. Once marked reserved the OS
145 * cannot use the memory. This is a bit of an odd place to reserve
146 * the region, but the CPU devices don't have dev_ops->read_resources()
147 * called on them.
148 *
149 * The range 0xa0000 -> 0xc0000 does not have any resources
150 * associated with it to handle legacy VGA memory. If this range
151 * is not omitted the mtrr code will setup the area as cacheable
152 * causing VGA access to not work.
153 *
154 * The TSEG region is mapped as cacheable so that one can perform
155 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
156 * precedence over the existing MTRRs covering this region.
157 *
158 * It should be noted that cacheable entry types need to be added in
159 * order. The reason is that the current MTRR code assumes this and
160 * falls over itself if it isn't.
161 *
162 * The resource index starts low and should not meet or exceed
163 * PCI_BASE_ADDRESS_0.
164 */
165static void sa_add_dram_resources(struct device *dev, int *resource_count)
166{
167 uintptr_t base_k, touud_k;
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200168 size_t size_k;
Subrata Banik7609c652017-05-19 14:50:09 +0530169 uint64_t sa_map_values[MAX_MAP_ENTRIES];
170 uintptr_t top_of_ram;
171 int index = *resource_count;
172
Subrata Banik7609c652017-05-19 14:50:09 +0530173 top_of_ram = (uintptr_t)cbmem_top();
174
175 /* 0 - > 0xa0000 */
176 base_k = 0;
177 size_k = (0xa0000 / KiB) - base_k;
178 ram_resource(dev, index++, base_k, size_k);
179
180 /* 0xc0000 -> top_of_ram */
181 base_k = 0xc0000 / KiB;
182 size_k = (top_of_ram / KiB) - base_k;
183 ram_resource(dev, index++, base_k, size_k);
184
185 sa_get_mem_map(dev, &sa_map_values[0]);
186
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200187 /* top_of_ram -> BGSM */
Subrata Banik7609c652017-05-19 14:50:09 +0530188 base_k = top_of_ram;
Subrata Banik7609c652017-05-19 14:50:09 +0530189 size_k = sa_map_values[SA_BGSM_REG] - base_k;
190 reserved_ram_resource(dev, index++, base_k / KiB, size_k / KiB);
191
192 /* BGSM -> TOLUD */
193 base_k = sa_map_values[SA_BGSM_REG];
194 size_k = sa_map_values[SA_TOLUD_REG] - base_k;
195 mmio_resource(dev, index++, base_k / KiB, size_k / KiB);
196
197 /* 4GiB -> TOUUD */
198 base_k = 4 * (GiB / KiB); /* 4GiB */
199 touud_k = sa_map_values[SA_TOUUD_REG] / KiB;
200 size_k = touud_k - base_k;
201 if (touud_k > base_k)
202 ram_resource(dev, index++, base_k, size_k);
203
204 /*
205 * Reserve everything between A segment and 1MB:
206 *
207 * 0xa0000 - 0xbffff: legacy VGA
208 * 0xc0000 - 0xfffff: RAM
209 */
210 mmio_resource(dev, index++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
211 reserved_ram_resource(dev, index++, 0xc0000 / KiB,
212 (1*MiB - 0xc0000) / KiB);
213
214 *resource_count = index;
215}
216
217static bool is_imr_enabled(uint32_t imr_base_reg)
218{
219 return !!(imr_base_reg & (1 << 31));
220}
221
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200222static void imr_resource(struct device *dev, int idx, uint32_t base,
223 uint32_t mask)
Subrata Banik7609c652017-05-19 14:50:09 +0530224{
225 uint32_t base_k, size_k;
226 /* Bits 28:0 encode the base address bits 38:10, hence the KiB unit. */
227 base_k = (base & 0x0fffffff);
228 /* Bits 28:0 encode the AND mask used for comparison, in KiB. */
229 size_k = ((~mask & 0x0fffffff) + 1);
230 /*
231 * IMRs sit in lower DRAM. Mark them cacheable, otherwise we run
232 * out of MTRRs. Memory reserved by IMRs is not usable for host
233 * so mark it reserved.
234 */
235 reserved_ram_resource(dev, idx, base_k, size_k);
236}
237
238/*
239 * Add IMR ranges that hang off the host bridge/memory
240 * controller device in case CONFIG_SA_ENABLE_IMR is selected by SoC.
241 */
242static void sa_add_imr_resources(struct device *dev, int *resource_cnt)
243{
244 size_t i, imr_offset;
245 uint32_t base, mask;
246 int index = *resource_cnt;
247
248 for (i = 0; i < MCH_NUM_IMRS; i++) {
249 imr_offset = i * MCH_IMR_PITCH;
250 base = MCHBAR32(imr_offset + MCH_IMR0_BASE);
251 mask = MCHBAR32(imr_offset + MCH_IMR0_MASK);
252
253 if (is_imr_enabled(base))
254 imr_resource(dev, index++, base, mask);
255 }
256
257 *resource_cnt = index;
258}
259
260static void systemagent_read_resources(struct device *dev)
261{
262 int index = 0;
263
264 /* Read standard PCI resources. */
265 pci_dev_read_resources(dev);
266
267 /* Add all fixed MMIO resources. */
268 soc_add_fixed_mmio_resources(dev, &index);
269 /* Calculate and add DRAM resources. */
270 sa_add_dram_resources(dev, &index);
Julius Wernercd49cce2019-03-05 16:53:33 -0800271 if (CONFIG(SA_ENABLE_IMR))
Subrata Banik7609c652017-05-19 14:50:09 +0530272 /* Add the isolated memory ranges (IMRs). */
273 sa_add_imr_resources(dev, &index);
274}
275
Lijian Zhao357e5522019-04-11 13:07:00 -0700276#if CONFIG(GENERATE_SMBIOS_TABLES)
277static int sa_smbios_write_type_16(struct device *dev, int *handle,
278 unsigned long *current)
279{
280 struct smbios_type16 *t = (struct smbios_type16 *)*current;
281 int len = sizeof(struct smbios_type16);
282
283 struct memory_info *meminfo;
284 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
285 if (meminfo == NULL)
286 return 0; /* can't find mem info in cbmem */
287
288 memset(t, 0, sizeof(struct smbios_type16));
289 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
290 t->handle = *handle;
291 t->length = len - 2;
292 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
293 t->use = MEMORY_ARRAY_USE_SYSTEM;
294 /* TBD, meminfo hob have information about ECC */
295 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
296 /* no error information handle available */
297 t->memory_error_information_handle = 0xFFFE;
298 t->maximum_capacity = 32 * (GiB / KiB); /* 32GB as default */
299 t->number_of_memory_devices = meminfo->dimm_cnt;
300
301 *current += len;
302 *handle += 1;
303 return len;
304}
305#endif
306
Subrata Banik7609c652017-05-19 14:50:09 +0530307void enable_power_aware_intr(void)
308{
309 uint8_t pair;
310
311 /* Enable Power Aware Interrupt Routing */
312 pair = MCHBAR8(MCH_PAIR);
313 pair &= ~0x7; /* Clear 2:0 */
314 pair |= 0x4; /* Fixed Priority */
315 MCHBAR8(MCH_PAIR) = pair;
316}
317
318static 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,
Subrata Banik7609c652017-05-19 14:50:09 +0530322 .init = 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
Lijian Zhao357e5522019-04-11 13:07:00 -0700327#if CONFIG(GENERATE_SMBIOS_TABLES)
328 .get_smbios_data = sa_smbios_write_type_16,
329#endif
Subrata Banik7609c652017-05-19 14:50:09 +0530330};
331
332static const unsigned short systemagent_ids[] = {
333 PCI_DEVICE_ID_INTEL_GLK_NB,
334 PCI_DEVICE_ID_INTEL_APL_NB,
Lijian Zhaobbedef92017-07-29 16:38:38 -0700335 PCI_DEVICE_ID_INTEL_CNL_ID_U,
336 PCI_DEVICE_ID_INTEL_CNL_ID_Y,
Subrata Banik7609c652017-05-19 14:50:09 +0530337 PCI_DEVICE_ID_INTEL_SKL_ID_U,
338 PCI_DEVICE_ID_INTEL_SKL_ID_Y,
339 PCI_DEVICE_ID_INTEL_SKL_ID_ULX,
Maxim Polyakovdde937c2019-09-09 15:50:03 +0300340 PCI_DEVICE_ID_INTEL_SKL_ID_H_4,
Keno Fischer1044eba2019-06-07 01:55:56 -0400341 PCI_DEVICE_ID_INTEL_SKL_ID_H_2,
342 PCI_DEVICE_ID_INTEL_SKL_ID_S_2,
343 PCI_DEVICE_ID_INTEL_SKL_ID_S_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800344 PCI_DEVICE_ID_INTEL_WHL_ID_W_2,
345 PCI_DEVICE_ID_INTEL_WHL_ID_W_4,
Gaggery Tsaie415a4c2018-03-21 22:36:18 +0800346 PCI_DEVICE_ID_INTEL_KBL_ID_S,
Subrata Banik7609c652017-05-19 14:50:09 +0530347 PCI_DEVICE_ID_INTEL_SKL_ID_H_EM,
348 PCI_DEVICE_ID_INTEL_KBL_ID_U,
349 PCI_DEVICE_ID_INTEL_KBL_ID_Y,
350 PCI_DEVICE_ID_INTEL_KBL_ID_H,
351 PCI_DEVICE_ID_INTEL_KBL_U_R,
V Sowmyaacc2a482018-01-23 15:27:23 +0530352 PCI_DEVICE_ID_INTEL_KBL_ID_DT,
Christian Walter3d840382019-05-17 19:37:16 +0200353 PCI_DEVICE_ID_INTEL_KBL_ID_DT_2,
Maulikfc19ab52018-01-05 22:40:35 +0530354 PCI_DEVICE_ID_INTEL_CFL_ID_U,
Christian Walterccac15a2019-08-13 09:55:37 +0200355 PCI_DEVICE_ID_INTEL_CFL_ID_U_2,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800356 PCI_DEVICE_ID_INTEL_CFL_ID_H,
Christian Walterccac15a2019-08-13 09:55:37 +0200357 PCI_DEVICE_ID_INTEL_CFL_ID_H_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800358 PCI_DEVICE_ID_INTEL_CFL_ID_H_8,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800359 PCI_DEVICE_ID_INTEL_CFL_ID_S,
Christian Walterccac15a2019-08-13 09:55:37 +0200360 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_2,
Felix Singerd298ffe2019-07-28 13:27:11 +0200361 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800362 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_8,
Christian Walterccac15a2019-08-13 09:55:37 +0200363 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_4,
364 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_6,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800365 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_8,
Christian Walterccac15a2019-08-13 09:55:37 +0200366 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_4,
367 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_6,
368 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_8,
Aamir Bohra9eac0392018-06-30 12:07:04 +0530369 PCI_DEVICE_ID_INTEL_ICL_ID_U,
370 PCI_DEVICE_ID_INTEL_ICL_ID_U_2_2,
371 PCI_DEVICE_ID_INTEL_ICL_ID_Y,
372 PCI_DEVICE_ID_INTEL_ICL_ID_Y_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530373 PCI_DEVICE_ID_INTEL_CML_ULT,
Subrata Banikba8af582019-02-27 15:00:55 +0530374 PCI_DEVICE_ID_INTEL_CML_ULT_2_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530375 PCI_DEVICE_ID_INTEL_CML_ULT_6_2,
376 PCI_DEVICE_ID_INTEL_CML_ULX,
377 PCI_DEVICE_ID_INTEL_CML_S,
Gaggery Tsaifdcc9ab2019-11-04 20:49:10 -0800378 PCI_DEVICE_ID_INTEL_CML_S_G0G1_P0P1_6_2,
379 PCI_DEVICE_ID_INTEL_CML_S_P0P1_8_2,
380 PCI_DEVICE_ID_INTEL_CML_S_P0P1_10_2,
Gaggery Tsai39e1f442020-01-08 15:22:13 -0800381 PCI_DEVICE_ID_INTEL_CML_S_G0G1_4,
382 PCI_DEVICE_ID_INTEL_CML_S_G0G1_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530383 PCI_DEVICE_ID_INTEL_CML_H,
Jamie Chen6bb9aaf2019-12-20 19:30:33 +0800384 PCI_DEVICE_ID_INTEL_CML_H_4_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530385 PCI_DEVICE_ID_INTEL_CML_H_8_2,
Ravi Sarawadi6b5bf402019-10-21 22:25:04 -0700386 PCI_DEVICE_ID_INTEL_TGL_ID_U,
Subrata Banikae695752019-11-12 12:47:43 +0530387 PCI_DEVICE_ID_INTEL_TGL_ID_U_1,
Srinidhi N Kaushik1d812e82020-02-07 15:51:09 -0800388 PCI_DEVICE_ID_INTEL_TGL_ID_U_2_2,
Ravi Sarawadi6b5bf402019-10-21 22:25:04 -0700389 PCI_DEVICE_ID_INTEL_TGL_ID_Y,
Tan, Lean Sheng26136092020-01-20 19:13:56 -0800390 PCI_DEVICE_ID_INTEL_JSL_EHL,
391 PCI_DEVICE_ID_INTEL_EHL_ID_1,
Meera Ravindranath3f4af0d2020-02-12 16:01:22 +0530392 PCI_DEVICE_ID_INTEL_JSL_ID_1,
Subrata Banik7609c652017-05-19 14:50:09 +0530393 0
394};
395
396static const struct pci_driver systemagent_driver __pci_driver = {
397 .ops = &systemagent_ops,
398 .vendor = PCI_VENDOR_ID_INTEL,
399 .devices = systemagent_ids
400};