blob: eaf1e2918a44ef021bea456a6c6f0c3e9906da17 [file] [log] [blame]
Patrick Georgi02363b52020-05-05 20:48:50 +02001/* This file is part of the coreboot project. */
Patrick Georgiac959032020-05-05 22:49:26 +02002/* SPDX-License-Identifier: GPL-2.0-or-later */
Elyes HAOUASa1e22b82019-03-18 22:49:36 +01003
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpigen.h>
Shaunak Sahabd427802017-07-18 00:19:33 -07005#include <arch/ioapic.h>
6#include <arch/smp/mpspec.h>
7#include <bootstate.h>
8#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02009#include <cf9_reset.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
Aaron Durbin64031672018-04-21 14:45:32 -060093__weak void soc_fill_fadt(acpi_fadt_t *fadt)
Shaunak Sahabd427802017-07-18 00:19:33 -070094{
95}
96
97void acpi_fill_fadt(acpi_fadt_t *fadt)
98{
99 const uint16_t pmbase = ACPI_BASE_ADDRESS;
100
Marc Jonesf9ea7ed2018-08-22 18:59:26 -0600101 fadt->header.revision = get_acpi_table_revision(FADT);
Shaunak Sahabd427802017-07-18 00:19:33 -0700102
103 fadt->sci_int = acpi_sci_irq();
104 fadt->smi_cmd = APM_CNT;
105 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
106 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
107 fadt->s4bios_req = 0x0;
108 fadt->pstate_cnt = 0;
109
110 fadt->pm1a_evt_blk = pmbase + PM1_STS;
111 fadt->pm1b_evt_blk = 0x0;
112 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
113 fadt->pm1b_cnt_blk = 0x0;
114
115 fadt->gpe0_blk = pmbase + GPE0_STS(0);
116
117 fadt->pm1_evt_len = 4;
118 fadt->pm1_cnt_len = 2;
119
120 /* GPE0 STS/EN pairs each 32 bits wide. */
121 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
122
123 fadt->flush_size = 0x400; /* twice of cache size */
124 fadt->flush_stride = 0x10; /* Cache line width */
125 fadt->duty_offset = 1;
126 fadt->day_alrm = 0xd;
127
128 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
129 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
130 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
131 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
132
133 fadt->reset_reg.space_id = 1;
134 fadt->reset_reg.bit_width = 8;
135 fadt->reset_reg.addrl = RST_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100136 fadt->reset_reg.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700137 fadt->reset_value = RST_CPU | SYS_RST;
138
139 fadt->x_pm1a_evt_blk.space_id = 1;
140 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
141 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100142 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
143
Shaunak Sahabd427802017-07-18 00:19:33 -0700144
145 fadt->x_pm1b_evt_blk.space_id = 1;
146
147 fadt->x_pm1a_cnt_blk.space_id = 1;
148 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
149 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100150 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700151
152 fadt->x_pm1b_cnt_blk.space_id = 1;
153
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100154 /*
155 * Windows 10 requires x_gpe0_blk to be set starting with FADT revision 5.
156 * The bit_width field intentionally overflows here.
157 * The OSPM can instead use the values in `fadt->gpe0_blk{,_len}`, which
158 * seems to work fine on Linux 5.0 and Windows 10.
159 */
160 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
161 fadt->x_gpe0_blk.bit_width = fadt->gpe0_blk_len * 8;
162 fadt->x_gpe0_blk.bit_offset = 0;
163 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
164 fadt->x_gpe0_blk.addrl = fadt->gpe0_blk;
165 fadt->x_gpe0_blk.addrh = 0;
166
Shaunak Sahabd427802017-07-18 00:19:33 -0700167 fadt->x_gpe1_blk.space_id = 1;
168
169 soc_fill_fadt(fadt);
170}
171
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700172unsigned long southbridge_write_acpi_tables(const struct device *device,
Shaunak Sahabd427802017-07-18 00:19:33 -0700173 unsigned long current,
174 struct acpi_rsdp *rsdp)
175{
Duncan Laurie93bbd412017-11-11 20:03:29 -0800176 current = acpi_write_dbg2_pci_uart(rsdp, current,
Subrata Banikafa07f72018-05-24 12:21:06 +0530177 uart_get_device(),
Duncan Laurie93bbd412017-11-11 20:03:29 -0800178 ACPI_ACCESS_SIZE_DWORD_ACCESS);
Shaunak Sahabd427802017-07-18 00:19:33 -0700179 return acpi_write_hpet(device, current, rsdp);
180}
181
Aaron Durbin64031672018-04-21 14:45:32 -0600182__weak
Shaunak Sahabd427802017-07-18 00:19:33 -0700183uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
184 const struct chipset_power_state *ps)
185{
186 return generic_pm1_en;
187}
188
Julius Wernercd49cce2019-03-05 16:53:33 -0800189#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700190/*
191 * Save wake source information for calculating ACPI _SWS values
192 *
193 * @pm1: PM1_STS register with only enabled events set
194 * @gpe0: GPE0_STS registers with only enabled events set
195 *
196 * return the number of registers in the gpe0 array or -1 if nothing
197 * is provided by this function.
198 */
199
200static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
201{
202 struct chipset_power_state *ps;
203 static uint32_t gpe0_sts[GPE0_REG_MAX];
204 uint32_t pm1_en;
205 int i;
206
207 ps = cbmem_find(CBMEM_ID_POWER_STATE);
208 if (ps == NULL)
209 return -1;
210
211 /*
212 * PM1_EN to check the basic wake events which can happen through
213 * powerbtn or any other wake source like lidopen, key board press etc.
214 */
215 pm1_en = ps->pm1_en;
216
217 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
218
219 *pm1 = ps->pm1_sts & pm1_en;
220
221 /* Mask off GPE0 status bits that are not enabled */
222 *gpe0 = &gpe0_sts[0];
223 for (i = 0; i < GPE0_REG_MAX; i++)
224 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
225
226 return GPE0_REG_MAX;
227}
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200228#endif
Shaunak Sahabd427802017-07-18 00:19:33 -0700229
Aaron Durbin64031672018-04-21 14:45:32 -0600230__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700231{
232}
233
Furquan Shaikh338fd9a2020-04-24 22:57:05 -0700234void southbridge_inject_dsdt(const struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700235{
236 struct global_nvs_t *gnvs;
237
238 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
239 if (!gnvs) {
240 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
241 if (gnvs)
242 memset(gnvs, 0, sizeof(*gnvs));
243 }
244
245 if (gnvs) {
246 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700247 /* And tell SMI about it */
248 smm_setup_structures(gnvs, NULL, NULL);
249
250 /* Add it to DSDT. */
251 acpigen_write_scope("\\");
252 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
253 acpigen_pop_len();
254 }
255}
256
257static int calculate_power(int tdp, int p1_ratio, int ratio)
258{
259 u32 m;
260 u32 power;
261
262 /*
263 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
264 *
265 * Power = (ratio / p1_ratio) * m * tdp
266 */
267
268 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
269 m = (m * m) / 1000;
270
271 power = ((ratio * 100000 / p1_ratio) / 100);
272 power *= (m / 100) * (tdp / 1000);
273 power /= 1000;
274
275 return power;
276}
277
278static int get_cores_per_package(void)
279{
280 struct cpuinfo_x86 c;
281 struct cpuid_result result;
282 int cores = 1;
283
284 get_fms(&c, cpuid_eax(1));
285 if (c.x86 != 6)
286 return 1;
287
288 result = cpuid_ext(0xb, 1);
289 cores = result.ebx & 0xff;
290
291 return cores;
292}
293
294static void generate_c_state_entries(void)
295{
296 acpi_cstate_t *c_state_map;
297 size_t entries;
298
299 c_state_map = soc_get_cstate_map(&entries);
300
301 /* Generate C-state tables */
302 acpigen_write_CST_package(c_state_map, entries);
303}
304
305void generate_p_state_entries(int core, int cores_per_package)
306{
307 int ratio_min, ratio_max, ratio_turbo, ratio_step;
308 int coord_type, power_max, num_entries;
309 int ratio, power, clock, clock_max;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100310 bool turbo;
Shaunak Sahabd427802017-07-18 00:19:33 -0700311
312 coord_type = cpu_get_coord_type();
313 ratio_min = cpu_get_min_ratio();
314 ratio_max = cpu_get_max_ratio();
315 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100316 turbo = (get_turbo_state() == TURBO_ENABLED);
Shaunak Sahabd427802017-07-18 00:19:33 -0700317
318 /* Calculate CPU TDP in mW */
319 power_max = cpu_get_power_max();
320
321 /* Write _PCT indicating use of FFixedHW */
322 acpigen_write_empty_PCT();
323
324 /* Write _PPC with no limit on supported P-state */
325 acpigen_write_PPC_NVS();
326 /* Write PSD indicating configured coordination type */
327 acpigen_write_PSD_package(core, 1, coord_type);
328
329 /* Add P-state entries in _PSS table */
330 acpigen_write_name("_PSS");
331
332 /* Determine ratio points */
333 ratio_step = PSS_RATIO_STEP;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100334 do {
Shaunak Sahabd427802017-07-18 00:19:33 -0700335 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100336 if (((ratio_max - ratio_min) % ratio_step) > 0)
337 num_entries += 1;
338 if (turbo)
339 num_entries += 1;
340 if (num_entries > PSS_MAX_ENTRIES)
341 ratio_step += 1;
342 } while (num_entries > PSS_MAX_ENTRIES);
343
344 /* _PSS package count depends on Turbo */
345 acpigen_write_package(num_entries);
Shaunak Sahabd427802017-07-18 00:19:33 -0700346
347 /* P[T] is Turbo state if enabled */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100348 if (turbo) {
Shaunak Sahabd427802017-07-18 00:19:33 -0700349 ratio_turbo = cpu_get_max_turbo_ratio();
350
351 /* Add entry for Turbo ratio */
352 acpigen_write_PSS_package(clock_max + 1, /* MHz */
353 power_max, /* mW */
354 PSS_LATENCY_TRANSITION,/* lat1 */
355 PSS_LATENCY_BUSMASTER,/* lat2 */
356 ratio_turbo << 8, /* control */
357 ratio_turbo << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100358 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700359 }
360
361 /* First regular entry is max non-turbo ratio */
362 acpigen_write_PSS_package(clock_max, /* MHz */
363 power_max, /* mW */
364 PSS_LATENCY_TRANSITION,/* lat1 */
365 PSS_LATENCY_BUSMASTER,/* lat2 */
366 ratio_max << 8, /* control */
367 ratio_max << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100368 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700369
370 /* Generate the remaining entries */
371 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
372 ratio >= ratio_min; ratio -= ratio_step) {
373
374 /* Calculate power at this ratio */
375 power = calculate_power(power_max, ratio_max, ratio);
376 clock = (ratio * cpu_get_bus_clock()) / KHz;
377
378 acpigen_write_PSS_package(clock, /* MHz */
379 power, /* mW */
380 PSS_LATENCY_TRANSITION,/* lat1 */
381 PSS_LATENCY_BUSMASTER,/* lat2 */
382 ratio << 8, /* control */
383 ratio << 8); /* status */
384 }
385 /* Fix package length */
386 acpigen_pop_len();
387}
388
Julien Viard de Galbert595202c2018-03-29 14:01:01 +0200389__attribute__ ((weak)) acpi_tstate_t *soc_get_tss_table(int *entries)
Shaunak Sahabd427802017-07-18 00:19:33 -0700390{
391 *entries = 0;
392 return NULL;
393}
394
395void generate_t_state_entries(int core, int cores_per_package)
396{
397 acpi_tstate_t *soc_tss_table;
398 int entries;
399
400 soc_tss_table = soc_get_tss_table(&entries);
401 if (entries == 0)
402 return;
403
404 /* Indicate SW_ALL coordination for T-states */
405 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
406
407 /* Indicate FixedHW so OS will use MSR */
408 acpigen_write_empty_PTC();
409
410 /* Set NVS controlled T-state limit */
411 acpigen_write_TPC("\\TLVL");
412
413 /* Write TSS table for MSR access */
414 acpigen_write_TSS_package(entries, soc_tss_table);
415}
416
Aaron Durbin64031672018-04-21 14:45:32 -0600417__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700418 int cores_per_package)
419{
420}
421
Furquan Shaikh7536a392020-04-24 21:59:21 -0700422void generate_cpu_entries(const struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700423{
424 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
425 int plen = 6;
426 int totalcores = dev_count_cpu();
427 int cores_per_package = get_cores_per_package();
428 int numcpus = totalcores / cores_per_package;
429
430 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
431 numcpus, cores_per_package);
432
433 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
434 for (core_id = 0; core_id < cores_per_package; core_id++) {
435 if (core_id > 0) {
436 pcontrol_blk = 0;
437 plen = 0;
438 }
439
Christian Walterbe3979c2019-12-18 15:07:59 +0100440 /* Generate processor \_SB.CPUx */
Shaunak Sahabd427802017-07-18 00:19:33 -0700441 acpigen_write_processor((cpu_id) * cores_per_package +
442 core_id, pcontrol_blk, plen);
443
444 /* Generate C-state tables */
445 generate_c_state_entries();
446
447 /* Soc specific power states generation */
448 soc_power_states_generation(core_id, cores_per_package);
449
450 acpigen_pop_len();
451 }
452 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100453 /* PPKG is usually used for thermal management
454 of the first and only package. */
455 acpigen_write_processor_package("PPKG", 0, cores_per_package);
456
457 /* Add a method to notify processor nodes */
458 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700459}
460
Julius Wernercd49cce2019-03-05 16:53:33 -0800461#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700462/* Save wake source data for ACPI _SWS methods in NVS */
463static void acpi_save_wake_source(void *unused)
464{
465 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
466 uint32_t pm1, *gpe0;
467 int gpe_reg, gpe_reg_count;
468 int reg_size = sizeof(uint32_t) * 8;
469
470 if (!gnvs)
471 return;
472
473 gnvs->pm1i = -1;
474 gnvs->gpei = -1;
475
476 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
477 if (gpe_reg_count < 0)
478 return;
479
480 /* Scan for first set bit in PM1 */
481 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
482 if (pm1 & 1)
483 break;
484 pm1 >>= 1;
485 }
486
487 /* If unable to determine then return -1 */
488 if (gnvs->pm1i >= 16)
489 gnvs->pm1i = -1;
490
491 /* Scan for first set bit in GPE registers */
492 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
493 uint32_t gpe = gpe0[gpe_reg];
494 int start = gpe_reg * reg_size;
495 int end = start + reg_size;
496
497 if (gpe == 0) {
498 if (!gnvs->gpei)
499 gnvs->gpei = end;
500 continue;
501 }
502
503 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
504 if (gpe & 1)
505 break;
506 gpe >>= 1;
507 }
508 }
509
510 /* If unable to determine then return -1 */
511 if (gnvs->gpei >= gpe_reg_count * reg_size)
512 gnvs->gpei = -1;
513
514 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
515 (long long)gnvs->pm1i, (long long)gnvs->gpei);
516}
517
518BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
519
520#endif