blob: 5da28007ee8dd570fe38d823bee545856ab44f7e [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>
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>
Patrick Rudolph5e007802020-07-27 15:37:43 +020016#include <types.h>
Subrata Banik7609c652017-05-19 14:50:09 +053017#include "systemagent_def.h"
Subrata Banik01ae11b2017-03-04 23:32:41 +053018
Subrata Banik7609c652017-05-19 14:50:09 +053019/* SoC override function */
Aaron Durbin64031672018-04-21 14:45:32 -060020__weak void soc_systemagent_init(struct device *dev)
Subrata Banik01ae11b2017-03-04 23:32:41 +053021{
Subrata Banik7609c652017-05-19 14:50:09 +053022 /* no-op */
Subrata Banik01ae11b2017-03-04 23:32:41 +053023}
24
Aaron Durbin64031672018-04-21 14:45:32 -060025__weak void soc_add_fixed_mmio_resources(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +053026 int *resource_cnt)
27{
28 /* no-op */
29}
30
Aaron Durbin64031672018-04-21 14:45:32 -060031__weak int soc_get_uncore_prmmr_base_and_mask(uint64_t *base,
Pratik Prajapati82cdfa72017-08-28 14:48:55 -070032 uint64_t *mask)
33{
34 /* return failure for this dummy API */
35 return -1;
36}
37
Furquan Shaikh0f007d82020-04-24 06:41:18 -070038__weak unsigned long sa_write_acpi_tables(const struct device *dev,
Werner Zehd12530c2018-12-14 13:09:12 +010039 unsigned long current,
40 struct acpi_rsdp *rsdp)
41{
42 return current;
43}
44
Patrick Rudolphbf72dcb2020-05-12 16:04:47 +020045__weak uint32_t soc_systemagent_max_chan_capacity_mib(u8 capid0_a_ddrsz)
46{
47 return 32768; /* 32 GiB per channel */
48}
49
Patrick Rudolph5e007802020-07-27 15:37:43 +020050static bool sa_supports_ecc(const uint32_t capid0_a)
51{
52 return !(capid0_a & CAPID_ECCDIS);
53}
54
55static size_t sa_slots_per_channel(const uint32_t capid0_a)
56{
57 return !(capid0_a & CAPID_DDPCD) + 1;
58}
59
60static size_t sa_number_of_channels(const uint32_t capid0_a)
61{
62 return !(capid0_a & CAPID_PDCD) + 1;
63}
64
65static void sa_soc_systemagent_init(struct device *dev)
66{
67 soc_systemagent_init(dev);
68
69 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
70 if (m == NULL)
71 return;
72
73 const uint32_t capid0_a = pci_read_config32(dev, CAPID0_A);
74
75 m->ecc_capable = sa_supports_ecc(capid0_a);
76 m->max_capacity_mib = soc_systemagent_max_chan_capacity_mib(CAPID_DDRSZ(capid0_a)) *
77 sa_number_of_channels(capid0_a);
78 m->number_of_devices = sa_slots_per_channel(capid0_a) *
79 sa_number_of_channels(capid0_a);
80}
81
Subrata Banik7609c652017-05-19 14:50:09 +053082/*
83 * Add all known fixed MMIO ranges that hang off the host bridge/memory
84 * controller device.
85 */
86void sa_add_fixed_mmio_resources(struct device *dev, int *resource_cnt,
87 const struct sa_mmio_descriptor *sa_fixed_resources, size_t count)
88{
89 int i;
90 int index = *resource_cnt;
91
92 for (i = 0; i < count; i++) {
93 uintptr_t base;
94 size_t size;
95
96 size = sa_fixed_resources[i].size;
97 base = sa_fixed_resources[i].base;
98
99 mmio_resource(dev, index++, base / KiB, size / KiB);
100 }
101
102 *resource_cnt = index;
103}
104
105/*
106 * DRAM memory mapped register
107 *
108 * TOUUD: This 64 bit register defines the Top of Upper Usable DRAM
109 * TOLUD: This 32 bit register defines the Top of Low Usable DRAM
110 * BGSM: This register contains the base address of stolen DRAM memory for GTT
111 * TSEG: This register contains the base address of TSEG DRAM memory
112 */
113static const struct sa_mem_map_descriptor sa_memory_map[MAX_MAP_ENTRIES] = {
114 { TOUUD, true, "TOUUD" },
115 { TOLUD, false, "TOLUD" },
116 { BGSM, false, "BGSM" },
117 { TSEG, false, "TSEG" },
118};
119
120/* Read DRAM memory map register value through PCI configuration space */
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200121static void sa_read_map_entry(struct device *dev,
Subrata Banik7609c652017-05-19 14:50:09 +0530122 const struct sa_mem_map_descriptor *entry, uint64_t *result)
123{
124 uint64_t value = 0;
125
126 if (entry->is_64_bit) {
127 value = pci_read_config32(dev, entry->reg + 4);
128 value <<= 32;
129 }
130
131 value |= pci_read_config32(dev, entry->reg);
132 /* All registers are on a 1MiB granularity. */
133 value = ALIGN_DOWN(value, 1 * MiB);
134
135 *result = value;
136}
137
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700138/* Fill MMIO resource above 4GB into GNVS */
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300139void sa_fill_gnvs(struct global_nvs *gnvs)
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700140{
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700141 struct device *sa_dev = pcidev_path_on_root(SA_DEVFN_ROOT);
142
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700143 sa_read_map_entry(sa_dev, &sa_memory_map[SA_TOUUD_REG], &gnvs->a4gb);
Furquan Shaikhcc35f722020-05-12 16:25:31 -0700144 gnvs->a4gs = POWER_OF_2(cpu_phys_address_size()) - gnvs->a4gb;
145 printk(BIOS_DEBUG, "PCI space above 4GB MMIO is at 0x%llx, len = 0x%llx\n",
Furquan Shaikh1085fee2020-05-07 16:04:16 -0700146 gnvs->a4gb, gnvs->a4gs);
147}
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
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200161 * - top_of_ram -> BGSM: cacheable with standard MTRRs and reserved
Subrata Banik7609c652017-05-19 14:50:09 +0530162 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
163 * - 4GiB -> TOUUD: cacheable
164 *
165 * The default SMRAM space is reserved so that the range doesn't
166 * have to be saved during S3 Resume. Once marked reserved the OS
167 * cannot use the memory. This is a bit of an odd place to reserve
168 * the region, but the CPU devices don't have dev_ops->read_resources()
169 * called on them.
170 *
171 * The range 0xa0000 -> 0xc0000 does not have any resources
172 * associated with it to handle legacy VGA memory. If this range
173 * is not omitted the mtrr code will setup the area as cacheable
174 * causing VGA access to not work.
175 *
176 * The TSEG region is mapped as cacheable so that one can perform
177 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
178 * precedence over the existing MTRRs covering this region.
179 *
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
Michael Niewöhner40f893e2019-10-21 18:58:04 +0200209 /* top_of_ram -> BGSM */
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_BGSM_REG] - base_k;
212 reserved_ram_resource(dev, index++, base_k / KiB, size_k / KiB);
213
214 /* BGSM -> TOLUD */
215 base_k = sa_map_values[SA_BGSM_REG];
216 size_k = sa_map_values[SA_TOLUD_REG] - base_k;
217 mmio_resource(dev, index++, base_k / KiB, size_k / KiB);
218
219 /* 4GiB -> TOUUD */
220 base_k = 4 * (GiB / KiB); /* 4GiB */
221 touud_k = sa_map_values[SA_TOUUD_REG] / KiB;
222 size_k = touud_k - base_k;
223 if (touud_k > base_k)
224 ram_resource(dev, index++, base_k, size_k);
225
226 /*
227 * Reserve everything between A segment and 1MB:
228 *
229 * 0xa0000 - 0xbffff: legacy VGA
230 * 0xc0000 - 0xfffff: RAM
231 */
232 mmio_resource(dev, index++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
233 reserved_ram_resource(dev, index++, 0xc0000 / KiB,
234 (1*MiB - 0xc0000) / KiB);
235
236 *resource_count = index;
237}
238
239static bool is_imr_enabled(uint32_t imr_base_reg)
240{
241 return !!(imr_base_reg & (1 << 31));
242}
243
Elyes HAOUAS4a131262018-09-16 17:35:48 +0200244static void imr_resource(struct device *dev, int idx, uint32_t base,
245 uint32_t mask)
Subrata Banik7609c652017-05-19 14:50:09 +0530246{
247 uint32_t base_k, size_k;
248 /* Bits 28:0 encode the base address bits 38:10, hence the KiB unit. */
249 base_k = (base & 0x0fffffff);
250 /* Bits 28:0 encode the AND mask used for comparison, in KiB. */
251 size_k = ((~mask & 0x0fffffff) + 1);
252 /*
253 * IMRs sit in lower DRAM. Mark them cacheable, otherwise we run
254 * out of MTRRs. Memory reserved by IMRs is not usable for host
255 * so mark it reserved.
256 */
257 reserved_ram_resource(dev, idx, base_k, size_k);
258}
259
260/*
261 * Add IMR ranges that hang off the host bridge/memory
Martin Rothf48acbd2020-07-24 12:24:27 -0600262 * controller device in case CONFIG(SA_ENABLE_IMR) is selected by SoC.
Subrata Banik7609c652017-05-19 14:50:09 +0530263 */
264static void sa_add_imr_resources(struct device *dev, int *resource_cnt)
265{
266 size_t i, imr_offset;
267 uint32_t base, mask;
268 int index = *resource_cnt;
269
270 for (i = 0; i < MCH_NUM_IMRS; i++) {
271 imr_offset = i * MCH_IMR_PITCH;
272 base = MCHBAR32(imr_offset + MCH_IMR0_BASE);
273 mask = MCHBAR32(imr_offset + MCH_IMR0_MASK);
274
275 if (is_imr_enabled(base))
276 imr_resource(dev, index++, base, mask);
277 }
278
279 *resource_cnt = index;
280}
281
282static void systemagent_read_resources(struct device *dev)
283{
284 int index = 0;
285
286 /* Read standard PCI resources. */
287 pci_dev_read_resources(dev);
288
289 /* Add all fixed MMIO resources. */
290 soc_add_fixed_mmio_resources(dev, &index);
291 /* Calculate and add DRAM resources. */
292 sa_add_dram_resources(dev, &index);
Julius Wernercd49cce2019-03-05 16:53:33 -0800293 if (CONFIG(SA_ENABLE_IMR))
Subrata Banik7609c652017-05-19 14:50:09 +0530294 /* Add the isolated memory ranges (IMRs). */
295 sa_add_imr_resources(dev, &index);
296}
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
309static struct device_operations systemagent_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100310 .read_resources = systemagent_read_resources,
311 .set_resources = pci_dev_set_resources,
312 .enable_resources = pci_dev_enable_resources,
Patrick Rudolph5e007802020-07-27 15:37:43 +0200313 .init = sa_soc_systemagent_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530314 .ops_pci = &pci_dev_ops_pci,
Julius Wernercd49cce2019-03-05 16:53:33 -0800315#if CONFIG(HAVE_ACPI_TABLES)
Werner Zehd12530c2018-12-14 13:09:12 +0100316 .write_acpi_tables = sa_write_acpi_tables,
317#endif
Subrata Banik7609c652017-05-19 14:50:09 +0530318};
319
320static const unsigned short systemagent_ids[] = {
321 PCI_DEVICE_ID_INTEL_GLK_NB,
322 PCI_DEVICE_ID_INTEL_APL_NB,
Lijian Zhaobbedef92017-07-29 16:38:38 -0700323 PCI_DEVICE_ID_INTEL_CNL_ID_U,
324 PCI_DEVICE_ID_INTEL_CNL_ID_Y,
Subrata Banik7609c652017-05-19 14:50:09 +0530325 PCI_DEVICE_ID_INTEL_SKL_ID_U,
326 PCI_DEVICE_ID_INTEL_SKL_ID_Y,
327 PCI_DEVICE_ID_INTEL_SKL_ID_ULX,
Maxim Polyakovdde937c2019-09-09 15:50:03 +0300328 PCI_DEVICE_ID_INTEL_SKL_ID_H_4,
Keno Fischer1044eba2019-06-07 01:55:56 -0400329 PCI_DEVICE_ID_INTEL_SKL_ID_H_2,
330 PCI_DEVICE_ID_INTEL_SKL_ID_S_2,
331 PCI_DEVICE_ID_INTEL_SKL_ID_S_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800332 PCI_DEVICE_ID_INTEL_WHL_ID_W_2,
333 PCI_DEVICE_ID_INTEL_WHL_ID_W_4,
Gaggery Tsaie415a4c2018-03-21 22:36:18 +0800334 PCI_DEVICE_ID_INTEL_KBL_ID_S,
Subrata Banik7609c652017-05-19 14:50:09 +0530335 PCI_DEVICE_ID_INTEL_SKL_ID_H_EM,
336 PCI_DEVICE_ID_INTEL_KBL_ID_U,
337 PCI_DEVICE_ID_INTEL_KBL_ID_Y,
338 PCI_DEVICE_ID_INTEL_KBL_ID_H,
339 PCI_DEVICE_ID_INTEL_KBL_U_R,
V Sowmyaacc2a482018-01-23 15:27:23 +0530340 PCI_DEVICE_ID_INTEL_KBL_ID_DT,
Christian Walter3d840382019-05-17 19:37:16 +0200341 PCI_DEVICE_ID_INTEL_KBL_ID_DT_2,
Maulikfc19ab52018-01-05 22:40:35 +0530342 PCI_DEVICE_ID_INTEL_CFL_ID_U,
Christian Walterccac15a2019-08-13 09:55:37 +0200343 PCI_DEVICE_ID_INTEL_CFL_ID_U_2,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800344 PCI_DEVICE_ID_INTEL_CFL_ID_H,
Christian Walterccac15a2019-08-13 09:55:37 +0200345 PCI_DEVICE_ID_INTEL_CFL_ID_H_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800346 PCI_DEVICE_ID_INTEL_CFL_ID_H_8,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800347 PCI_DEVICE_ID_INTEL_CFL_ID_S,
Christian Walterccac15a2019-08-13 09:55:37 +0200348 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_2,
Felix Singerd298ffe2019-07-28 13:27:11 +0200349 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_4,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800350 PCI_DEVICE_ID_INTEL_CFL_ID_S_DT_8,
Christian Walterccac15a2019-08-13 09:55:37 +0200351 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_4,
352 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_6,
Lean Sheng Tan38c3ff72019-05-27 13:06:35 +0800353 PCI_DEVICE_ID_INTEL_CFL_ID_S_WS_8,
Christian Walterccac15a2019-08-13 09:55:37 +0200354 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_4,
355 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_6,
356 PCI_DEVICE_ID_INTEL_CFL_ID_S_S_8,
Aamir Bohra9eac0392018-06-30 12:07:04 +0530357 PCI_DEVICE_ID_INTEL_ICL_ID_U,
358 PCI_DEVICE_ID_INTEL_ICL_ID_U_2_2,
359 PCI_DEVICE_ID_INTEL_ICL_ID_Y,
360 PCI_DEVICE_ID_INTEL_ICL_ID_Y_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530361 PCI_DEVICE_ID_INTEL_CML_ULT,
Subrata Banikba8af582019-02-27 15:00:55 +0530362 PCI_DEVICE_ID_INTEL_CML_ULT_2_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530363 PCI_DEVICE_ID_INTEL_CML_ULT_6_2,
364 PCI_DEVICE_ID_INTEL_CML_ULX,
365 PCI_DEVICE_ID_INTEL_CML_S,
Gaggery Tsaifdcc9ab2019-11-04 20:49:10 -0800366 PCI_DEVICE_ID_INTEL_CML_S_G0G1_P0P1_6_2,
367 PCI_DEVICE_ID_INTEL_CML_S_P0P1_8_2,
368 PCI_DEVICE_ID_INTEL_CML_S_P0P1_10_2,
Gaggery Tsai39e1f442020-01-08 15:22:13 -0800369 PCI_DEVICE_ID_INTEL_CML_S_G0G1_4,
370 PCI_DEVICE_ID_INTEL_CML_S_G0G1_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530371 PCI_DEVICE_ID_INTEL_CML_H,
Jamie Chen6bb9aaf2019-12-20 19:30:33 +0800372 PCI_DEVICE_ID_INTEL_CML_H_4_2,
Ronak Kanabarf606a2f2019-02-04 16:06:50 +0530373 PCI_DEVICE_ID_INTEL_CML_H_8_2,
Srinidhi N Kaushik1d812e82020-02-07 15:51:09 -0800374 PCI_DEVICE_ID_INTEL_TGL_ID_U_2_2,
Derek Huang60f178d2020-07-03 15:33:13 +0800375 PCI_DEVICE_ID_INTEL_TGL_ID_U_4_2,
376 PCI_DEVICE_ID_INTEL_TGL_ID_Y_2_2,
377 PCI_DEVICE_ID_INTEL_TGL_ID_Y_4_2,
Tan, Lean Sheng26136092020-01-20 19:13:56 -0800378 PCI_DEVICE_ID_INTEL_JSL_EHL,
379 PCI_DEVICE_ID_INTEL_EHL_ID_1,
Meera Ravindranath3f4af0d2020-02-12 16:01:22 +0530380 PCI_DEVICE_ID_INTEL_JSL_ID_1,
Maulik V Vaghela8745a272020-04-22 12:13:40 +0530381 PCI_DEVICE_ID_INTEL_JSL_ID_2,
382 PCI_DEVICE_ID_INTEL_JSL_ID_3,
383 PCI_DEVICE_ID_INTEL_JSL_ID_4,
Subrata Banik7609c652017-05-19 14:50:09 +0530384 0
385};
386
387static const struct pci_driver systemagent_driver __pci_driver = {
388 .ops = &systemagent_ops,
389 .vendor = PCI_VENDOR_ID_INTEL,
390 .devices = systemagent_ids
391};