blob: 3265131f932e7a616ce69a5fa91929d0f82c86b7 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Elyes HAOUASa1e22b82019-03-18 22:49:36 +01002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpigen.h>
Shaunak Sahabd427802017-07-18 00:19:33 -07004#include <arch/ioapic.h>
5#include <arch/smp/mpspec.h>
6#include <bootstate.h>
7#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02008#include <cf9_reset.h>
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +03009#include <acpi/acpi_gnvs.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +010010#include <console/console.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070011#include <cpu/intel/turbo.h>
12#include <cpu/x86/msr.h>
13#include <cpu/x86/smm.h>
14#include <intelblocks/acpi.h>
15#include <intelblocks/msr.h>
16#include <intelblocks/pmclib.h>
Duncan Laurie93bbd412017-11-11 20:03:29 -080017#include <intelblocks/uart.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070018#include <soc/gpio.h>
19#include <soc/iomap.h>
20#include <soc/nvs.h>
21#include <soc/pm.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010022#include <string.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070023
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +020024__attribute__((weak)) unsigned long acpi_fill_mcfg(unsigned long current)
Shaunak Sahabd427802017-07-18 00:19:33 -070025{
26 /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */
27 current += acpi_create_mcfg_mmconfig((void *)current,
28 CONFIG_MMCONF_BASE_ADDRESS, 0, 0,
Duncan Lauriefd50b7c2018-03-02 14:47:11 -080029 (CONFIG_SA_PCIEX_LENGTH >> 20) - 1);
Shaunak Sahabd427802017-07-18 00:19:33 -070030 return current;
31}
32
33static int acpi_sci_irq(void)
34{
35 int sci_irq = 9;
36 uint32_t scis;
37
38 scis = soc_read_sci_irq_select();
39 scis &= SCI_IRQ_SEL;
40 scis >>= SCI_IRQ_ADJUST;
41
42 /* Determine how SCI is routed. */
43 switch (scis) {
44 case SCIS_IRQ9:
45 case SCIS_IRQ10:
46 case SCIS_IRQ11:
47 sci_irq = scis - SCIS_IRQ9 + 9;
48 break;
49 case SCIS_IRQ20:
50 case SCIS_IRQ21:
51 case SCIS_IRQ22:
52 case SCIS_IRQ23:
53 sci_irq = scis - SCIS_IRQ20 + 20;
54 break;
55 default:
56 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
57 sci_irq = 9;
58 break;
59 }
60
61 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
62 return sci_irq;
63}
64
65static unsigned long acpi_madt_irq_overrides(unsigned long current)
66{
67 int sci = acpi_sci_irq();
68 uint16_t flags = MP_IRQ_TRIGGER_LEVEL;
69
70 /* INT_SRC_OVR */
71 current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0);
72
73 flags |= soc_madt_sci_irq_polarity(sci);
74
75 /* SCI */
76 current +=
77 acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags);
78
79 return current;
80}
81
82unsigned long acpi_fill_madt(unsigned long current)
83{
84 /* Local APICs */
85 current = acpi_create_madt_lapics(current);
86
87 /* IOAPIC */
88 current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0);
89
90 return acpi_madt_irq_overrides(current);
91}
92
Shaunak Sahabd427802017-07-18 00:19:33 -070093void acpi_fill_fadt(acpi_fadt_t *fadt)
94{
95 const uint16_t pmbase = ACPI_BASE_ADDRESS;
96
Marc Jonesf9ea7ed2018-08-22 18:59:26 -060097 fadt->header.revision = get_acpi_table_revision(FADT);
Shaunak Sahabd427802017-07-18 00:19:33 -070098
99 fadt->sci_int = acpi_sci_irq();
Kyösti Mälkkic328a682019-11-23 07:23:40 +0200100
Kyösti Mälkki0a9e72e2019-08-11 01:22:28 +0300101 if (permanent_smi_handler()) {
Kyösti Mälkkic328a682019-11-23 07:23:40 +0200102 fadt->smi_cmd = APM_CNT;
103 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
104 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
105 }
Shaunak Sahabd427802017-07-18 00:19:33 -0700106
107 fadt->pm1a_evt_blk = pmbase + PM1_STS;
108 fadt->pm1b_evt_blk = 0x0;
109 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
110 fadt->pm1b_cnt_blk = 0x0;
111
112 fadt->gpe0_blk = pmbase + GPE0_STS(0);
113
114 fadt->pm1_evt_len = 4;
115 fadt->pm1_cnt_len = 2;
116
117 /* GPE0 STS/EN pairs each 32 bits wide. */
118 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
119
120 fadt->flush_size = 0x400; /* twice of cache size */
121 fadt->flush_stride = 0x10; /* Cache line width */
122 fadt->duty_offset = 1;
123 fadt->day_alrm = 0xd;
124
125 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
126 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
127 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
128 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
129
130 fadt->reset_reg.space_id = 1;
131 fadt->reset_reg.bit_width = 8;
132 fadt->reset_reg.addrl = RST_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100133 fadt->reset_reg.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700134 fadt->reset_value = RST_CPU | SYS_RST;
135
136 fadt->x_pm1a_evt_blk.space_id = 1;
137 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
138 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100139 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
140
Shaunak Sahabd427802017-07-18 00:19:33 -0700141
142 fadt->x_pm1b_evt_blk.space_id = 1;
143
144 fadt->x_pm1a_cnt_blk.space_id = 1;
145 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
146 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100147 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700148
149 fadt->x_pm1b_cnt_blk.space_id = 1;
150
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100151 /*
152 * Windows 10 requires x_gpe0_blk to be set starting with FADT revision 5.
153 * The bit_width field intentionally overflows here.
154 * The OSPM can instead use the values in `fadt->gpe0_blk{,_len}`, which
155 * seems to work fine on Linux 5.0 and Windows 10.
156 */
157 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
158 fadt->x_gpe0_blk.bit_width = fadt->gpe0_blk_len * 8;
159 fadt->x_gpe0_blk.bit_offset = 0;
Angel Ponsa23aff32020-06-21 20:47:54 +0200160 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100161 fadt->x_gpe0_blk.addrl = fadt->gpe0_blk;
162 fadt->x_gpe0_blk.addrh = 0;
163
Shaunak Sahabd427802017-07-18 00:19:33 -0700164 fadt->x_gpe1_blk.space_id = 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700165}
166
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700167unsigned long southbridge_write_acpi_tables(const struct device *device,
Shaunak Sahabd427802017-07-18 00:19:33 -0700168 unsigned long current,
169 struct acpi_rsdp *rsdp)
170{
Duncan Laurie93bbd412017-11-11 20:03:29 -0800171 current = acpi_write_dbg2_pci_uart(rsdp, current,
Subrata Banikafa07f72018-05-24 12:21:06 +0530172 uart_get_device(),
Duncan Laurie93bbd412017-11-11 20:03:29 -0800173 ACPI_ACCESS_SIZE_DWORD_ACCESS);
Shaunak Sahabd427802017-07-18 00:19:33 -0700174 return acpi_write_hpet(device, current, rsdp);
175}
176
Aaron Durbin64031672018-04-21 14:45:32 -0600177__weak
Shaunak Sahabd427802017-07-18 00:19:33 -0700178uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
179 const struct chipset_power_state *ps)
180{
181 return generic_pm1_en;
182}
183
Julius Wernercd49cce2019-03-05 16:53:33 -0800184#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700185/*
186 * Save wake source information for calculating ACPI _SWS values
187 *
188 * @pm1: PM1_STS register with only enabled events set
189 * @gpe0: GPE0_STS registers with only enabled events set
190 *
191 * return the number of registers in the gpe0 array or -1 if nothing
192 * is provided by this function.
193 */
194
195static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
196{
197 struct chipset_power_state *ps;
198 static uint32_t gpe0_sts[GPE0_REG_MAX];
199 uint32_t pm1_en;
200 int i;
201
202 ps = cbmem_find(CBMEM_ID_POWER_STATE);
203 if (ps == NULL)
204 return -1;
205
206 /*
207 * PM1_EN to check the basic wake events which can happen through
208 * powerbtn or any other wake source like lidopen, key board press etc.
209 */
210 pm1_en = ps->pm1_en;
211
212 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
213
214 *pm1 = ps->pm1_sts & pm1_en;
215
216 /* Mask off GPE0 status bits that are not enabled */
217 *gpe0 = &gpe0_sts[0];
218 for (i = 0; i < GPE0_REG_MAX; i++)
219 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
220
221 return GPE0_REG_MAX;
222}
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200223#endif
Shaunak Sahabd427802017-07-18 00:19:33 -0700224
Aaron Durbin64031672018-04-21 14:45:32 -0600225__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700226{
227}
228
Furquan Shaikh338fd9a2020-04-24 22:57:05 -0700229void southbridge_inject_dsdt(const struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700230{
231 struct global_nvs_t *gnvs;
232
233 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
234 if (!gnvs) {
235 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
236 if (gnvs)
237 memset(gnvs, 0, sizeof(*gnvs));
238 }
239
240 if (gnvs) {
241 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700242 /* And tell SMI about it */
Kyösti Mälkkic3c55212020-06-17 10:34:26 +0300243 apm_control(APM_CNT_GNVS_UPDATE);
Shaunak Sahabd427802017-07-18 00:19:33 -0700244
245 /* Add it to DSDT. */
246 acpigen_write_scope("\\");
247 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
248 acpigen_pop_len();
249 }
250}
251
252static int calculate_power(int tdp, int p1_ratio, int ratio)
253{
254 u32 m;
255 u32 power;
256
257 /*
258 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
259 *
260 * Power = (ratio / p1_ratio) * m * tdp
261 */
262
263 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
264 m = (m * m) / 1000;
265
266 power = ((ratio * 100000 / p1_ratio) / 100);
267 power *= (m / 100) * (tdp / 1000);
268 power /= 1000;
269
270 return power;
271}
272
273static int get_cores_per_package(void)
274{
275 struct cpuinfo_x86 c;
276 struct cpuid_result result;
277 int cores = 1;
278
279 get_fms(&c, cpuid_eax(1));
280 if (c.x86 != 6)
281 return 1;
282
283 result = cpuid_ext(0xb, 1);
284 cores = result.ebx & 0xff;
285
286 return cores;
287}
288
289static void generate_c_state_entries(void)
290{
291 acpi_cstate_t *c_state_map;
292 size_t entries;
293
294 c_state_map = soc_get_cstate_map(&entries);
295
296 /* Generate C-state tables */
297 acpigen_write_CST_package(c_state_map, entries);
298}
299
300void generate_p_state_entries(int core, int cores_per_package)
301{
302 int ratio_min, ratio_max, ratio_turbo, ratio_step;
303 int coord_type, power_max, num_entries;
304 int ratio, power, clock, clock_max;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100305 bool turbo;
Shaunak Sahabd427802017-07-18 00:19:33 -0700306
307 coord_type = cpu_get_coord_type();
308 ratio_min = cpu_get_min_ratio();
309 ratio_max = cpu_get_max_ratio();
310 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100311 turbo = (get_turbo_state() == TURBO_ENABLED);
Shaunak Sahabd427802017-07-18 00:19:33 -0700312
313 /* Calculate CPU TDP in mW */
314 power_max = cpu_get_power_max();
315
316 /* Write _PCT indicating use of FFixedHW */
317 acpigen_write_empty_PCT();
318
319 /* Write _PPC with no limit on supported P-state */
320 acpigen_write_PPC_NVS();
321 /* Write PSD indicating configured coordination type */
322 acpigen_write_PSD_package(core, 1, coord_type);
323
324 /* Add P-state entries in _PSS table */
325 acpigen_write_name("_PSS");
326
327 /* Determine ratio points */
328 ratio_step = PSS_RATIO_STEP;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100329 do {
Shaunak Sahabd427802017-07-18 00:19:33 -0700330 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100331 if (((ratio_max - ratio_min) % ratio_step) > 0)
332 num_entries += 1;
333 if (turbo)
334 num_entries += 1;
335 if (num_entries > PSS_MAX_ENTRIES)
336 ratio_step += 1;
337 } while (num_entries > PSS_MAX_ENTRIES);
338
339 /* _PSS package count depends on Turbo */
340 acpigen_write_package(num_entries);
Shaunak Sahabd427802017-07-18 00:19:33 -0700341
342 /* P[T] is Turbo state if enabled */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100343 if (turbo) {
Shaunak Sahabd427802017-07-18 00:19:33 -0700344 ratio_turbo = cpu_get_max_turbo_ratio();
345
346 /* Add entry for Turbo ratio */
347 acpigen_write_PSS_package(clock_max + 1, /* MHz */
348 power_max, /* mW */
349 PSS_LATENCY_TRANSITION,/* lat1 */
350 PSS_LATENCY_BUSMASTER,/* lat2 */
351 ratio_turbo << 8, /* control */
352 ratio_turbo << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100353 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700354 }
355
356 /* First regular entry is max non-turbo ratio */
357 acpigen_write_PSS_package(clock_max, /* MHz */
358 power_max, /* mW */
359 PSS_LATENCY_TRANSITION,/* lat1 */
360 PSS_LATENCY_BUSMASTER,/* lat2 */
361 ratio_max << 8, /* control */
362 ratio_max << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100363 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700364
365 /* Generate the remaining entries */
366 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
367 ratio >= ratio_min; ratio -= ratio_step) {
368
369 /* Calculate power at this ratio */
370 power = calculate_power(power_max, ratio_max, ratio);
371 clock = (ratio * cpu_get_bus_clock()) / KHz;
372
373 acpigen_write_PSS_package(clock, /* MHz */
374 power, /* mW */
375 PSS_LATENCY_TRANSITION,/* lat1 */
376 PSS_LATENCY_BUSMASTER,/* lat2 */
377 ratio << 8, /* control */
378 ratio << 8); /* status */
379 }
380 /* Fix package length */
381 acpigen_pop_len();
382}
383
Julien Viard de Galbert595202c2018-03-29 14:01:01 +0200384__attribute__ ((weak)) acpi_tstate_t *soc_get_tss_table(int *entries)
Shaunak Sahabd427802017-07-18 00:19:33 -0700385{
386 *entries = 0;
387 return NULL;
388}
389
390void generate_t_state_entries(int core, int cores_per_package)
391{
392 acpi_tstate_t *soc_tss_table;
393 int entries;
394
395 soc_tss_table = soc_get_tss_table(&entries);
396 if (entries == 0)
397 return;
398
399 /* Indicate SW_ALL coordination for T-states */
400 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
401
402 /* Indicate FixedHW so OS will use MSR */
403 acpigen_write_empty_PTC();
404
405 /* Set NVS controlled T-state limit */
406 acpigen_write_TPC("\\TLVL");
407
408 /* Write TSS table for MSR access */
409 acpigen_write_TSS_package(entries, soc_tss_table);
410}
411
Aaron Durbin64031672018-04-21 14:45:32 -0600412__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700413 int cores_per_package)
414{
415}
416
Furquan Shaikh7536a392020-04-24 21:59:21 -0700417void generate_cpu_entries(const struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700418{
419 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
420 int plen = 6;
421 int totalcores = dev_count_cpu();
422 int cores_per_package = get_cores_per_package();
423 int numcpus = totalcores / cores_per_package;
424
425 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
426 numcpus, cores_per_package);
427
428 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
429 for (core_id = 0; core_id < cores_per_package; core_id++) {
430 if (core_id > 0) {
431 pcontrol_blk = 0;
432 plen = 0;
433 }
434
Christian Walterbe3979c2019-12-18 15:07:59 +0100435 /* Generate processor \_SB.CPUx */
Shaunak Sahabd427802017-07-18 00:19:33 -0700436 acpigen_write_processor((cpu_id) * cores_per_package +
437 core_id, pcontrol_blk, plen);
438
439 /* Generate C-state tables */
440 generate_c_state_entries();
441
442 /* Soc specific power states generation */
443 soc_power_states_generation(core_id, cores_per_package);
444
445 acpigen_pop_len();
446 }
447 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100448 /* PPKG is usually used for thermal management
449 of the first and only package. */
450 acpigen_write_processor_package("PPKG", 0, cores_per_package);
451
452 /* Add a method to notify processor nodes */
453 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700454}
455
Julius Wernercd49cce2019-03-05 16:53:33 -0800456#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700457/* Save wake source data for ACPI _SWS methods in NVS */
458static void acpi_save_wake_source(void *unused)
459{
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +0300460 global_nvs_t *gnvs = acpi_get_gnvs();
Shaunak Sahabd427802017-07-18 00:19:33 -0700461 uint32_t pm1, *gpe0;
462 int gpe_reg, gpe_reg_count;
463 int reg_size = sizeof(uint32_t) * 8;
464
465 if (!gnvs)
466 return;
467
468 gnvs->pm1i = -1;
469 gnvs->gpei = -1;
470
471 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
472 if (gpe_reg_count < 0)
473 return;
474
475 /* Scan for first set bit in PM1 */
476 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
477 if (pm1 & 1)
478 break;
479 pm1 >>= 1;
480 }
481
482 /* If unable to determine then return -1 */
483 if (gnvs->pm1i >= 16)
484 gnvs->pm1i = -1;
485
486 /* Scan for first set bit in GPE registers */
487 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
488 uint32_t gpe = gpe0[gpe_reg];
489 int start = gpe_reg * reg_size;
490 int end = start + reg_size;
491
492 if (gpe == 0) {
493 if (!gnvs->gpei)
494 gnvs->gpei = end;
495 continue;
496 }
497
498 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
499 if (gpe & 1)
500 break;
501 gpe >>= 1;
502 }
503 }
504
505 /* If unable to determine then return -1 */
506 if (gnvs->gpei >= gpe_reg_count * reg_size)
507 gnvs->gpei = -1;
508
509 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
510 (long long)gnvs->pm1i, (long long)gnvs->gpei);
511}
512
513BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
514
515#endif