blob: f1c7b8a5a7b348a5c9ebb2168ce909b0cc873c74 [file] [log] [blame]
Shaunak Sahabd427802017-07-18 00:19:33 -07001/*
2 * This file is part of the coreboot project.
3 *
Shaunak Sahabd427802017-07-18 00:19:33 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010015
Shaunak Sahabd427802017-07-18 00:19:33 -070016#include <arch/acpigen.h>
17#include <arch/ioapic.h>
18#include <arch/smp/mpspec.h>
19#include <bootstate.h>
20#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +020021#include <cf9_reset.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +010022#include <console/console.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070023#include <cpu/intel/turbo.h>
24#include <cpu/x86/msr.h>
25#include <cpu/x86/smm.h>
26#include <intelblocks/acpi.h>
27#include <intelblocks/msr.h>
28#include <intelblocks/pmclib.h>
Duncan Laurie93bbd412017-11-11 20:03:29 -080029#include <intelblocks/uart.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070030#include <soc/gpio.h>
31#include <soc/iomap.h>
32#include <soc/nvs.h>
33#include <soc/pm.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010034#include <string.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070035
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +020036__attribute__((weak)) unsigned long acpi_fill_mcfg(unsigned long current)
Shaunak Sahabd427802017-07-18 00:19:33 -070037{
38 /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */
39 current += acpi_create_mcfg_mmconfig((void *)current,
40 CONFIG_MMCONF_BASE_ADDRESS, 0, 0,
Duncan Lauriefd50b7c2018-03-02 14:47:11 -080041 (CONFIG_SA_PCIEX_LENGTH >> 20) - 1);
Shaunak Sahabd427802017-07-18 00:19:33 -070042 return current;
43}
44
45static int acpi_sci_irq(void)
46{
47 int sci_irq = 9;
48 uint32_t scis;
49
50 scis = soc_read_sci_irq_select();
51 scis &= SCI_IRQ_SEL;
52 scis >>= SCI_IRQ_ADJUST;
53
54 /* Determine how SCI is routed. */
55 switch (scis) {
56 case SCIS_IRQ9:
57 case SCIS_IRQ10:
58 case SCIS_IRQ11:
59 sci_irq = scis - SCIS_IRQ9 + 9;
60 break;
61 case SCIS_IRQ20:
62 case SCIS_IRQ21:
63 case SCIS_IRQ22:
64 case SCIS_IRQ23:
65 sci_irq = scis - SCIS_IRQ20 + 20;
66 break;
67 default:
68 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
69 sci_irq = 9;
70 break;
71 }
72
73 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
74 return sci_irq;
75}
76
77static unsigned long acpi_madt_irq_overrides(unsigned long current)
78{
79 int sci = acpi_sci_irq();
80 uint16_t flags = MP_IRQ_TRIGGER_LEVEL;
81
82 /* INT_SRC_OVR */
83 current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0);
84
85 flags |= soc_madt_sci_irq_polarity(sci);
86
87 /* SCI */
88 current +=
89 acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags);
90
91 return current;
92}
93
94unsigned long acpi_fill_madt(unsigned long current)
95{
96 /* Local APICs */
97 current = acpi_create_madt_lapics(current);
98
99 /* IOAPIC */
100 current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0);
101
102 return acpi_madt_irq_overrides(current);
103}
104
Aaron Durbin64031672018-04-21 14:45:32 -0600105__weak void soc_fill_fadt(acpi_fadt_t *fadt)
Shaunak Sahabd427802017-07-18 00:19:33 -0700106{
107}
108
109void acpi_fill_fadt(acpi_fadt_t *fadt)
110{
111 const uint16_t pmbase = ACPI_BASE_ADDRESS;
112
Marc Jonesf9ea7ed2018-08-22 18:59:26 -0600113 fadt->header.revision = get_acpi_table_revision(FADT);
Shaunak Sahabd427802017-07-18 00:19:33 -0700114
115 fadt->sci_int = acpi_sci_irq();
116 fadt->smi_cmd = APM_CNT;
117 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
118 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
119 fadt->s4bios_req = 0x0;
120 fadt->pstate_cnt = 0;
121
122 fadt->pm1a_evt_blk = pmbase + PM1_STS;
123 fadt->pm1b_evt_blk = 0x0;
124 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
125 fadt->pm1b_cnt_blk = 0x0;
126
127 fadt->gpe0_blk = pmbase + GPE0_STS(0);
128
129 fadt->pm1_evt_len = 4;
130 fadt->pm1_cnt_len = 2;
131
132 /* GPE0 STS/EN pairs each 32 bits wide. */
133 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
134
135 fadt->flush_size = 0x400; /* twice of cache size */
136 fadt->flush_stride = 0x10; /* Cache line width */
137 fadt->duty_offset = 1;
138 fadt->day_alrm = 0xd;
139
140 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
141 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
142 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
143 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
144
145 fadt->reset_reg.space_id = 1;
146 fadt->reset_reg.bit_width = 8;
147 fadt->reset_reg.addrl = RST_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100148 fadt->reset_reg.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700149 fadt->reset_value = RST_CPU | SYS_RST;
150
151 fadt->x_pm1a_evt_blk.space_id = 1;
152 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
153 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100154 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
155
Shaunak Sahabd427802017-07-18 00:19:33 -0700156
157 fadt->x_pm1b_evt_blk.space_id = 1;
158
159 fadt->x_pm1a_cnt_blk.space_id = 1;
160 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
161 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100162 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Shaunak Sahabd427802017-07-18 00:19:33 -0700163
164 fadt->x_pm1b_cnt_blk.space_id = 1;
165
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100166 /*
167 * Windows 10 requires x_gpe0_blk to be set starting with FADT revision 5.
168 * The bit_width field intentionally overflows here.
169 * The OSPM can instead use the values in `fadt->gpe0_blk{,_len}`, which
170 * seems to work fine on Linux 5.0 and Windows 10.
171 */
172 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
173 fadt->x_gpe0_blk.bit_width = fadt->gpe0_blk_len * 8;
174 fadt->x_gpe0_blk.bit_offset = 0;
175 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
176 fadt->x_gpe0_blk.addrl = fadt->gpe0_blk;
177 fadt->x_gpe0_blk.addrh = 0;
178
Shaunak Sahabd427802017-07-18 00:19:33 -0700179 fadt->x_gpe1_blk.space_id = 1;
180
181 soc_fill_fadt(fadt);
182}
183
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700184unsigned long southbridge_write_acpi_tables(const struct device *device,
Shaunak Sahabd427802017-07-18 00:19:33 -0700185 unsigned long current,
186 struct acpi_rsdp *rsdp)
187{
Duncan Laurie93bbd412017-11-11 20:03:29 -0800188 current = acpi_write_dbg2_pci_uart(rsdp, current,
Subrata Banikafa07f72018-05-24 12:21:06 +0530189 uart_get_device(),
Duncan Laurie93bbd412017-11-11 20:03:29 -0800190 ACPI_ACCESS_SIZE_DWORD_ACCESS);
Shaunak Sahabd427802017-07-18 00:19:33 -0700191 return acpi_write_hpet(device, current, rsdp);
192}
193
Aaron Durbin64031672018-04-21 14:45:32 -0600194__weak
Shaunak Sahabd427802017-07-18 00:19:33 -0700195uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
196 const struct chipset_power_state *ps)
197{
198 return generic_pm1_en;
199}
200
Julius Wernercd49cce2019-03-05 16:53:33 -0800201#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700202/*
203 * Save wake source information for calculating ACPI _SWS values
204 *
205 * @pm1: PM1_STS register with only enabled events set
206 * @gpe0: GPE0_STS registers with only enabled events set
207 *
208 * return the number of registers in the gpe0 array or -1 if nothing
209 * is provided by this function.
210 */
211
212static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
213{
214 struct chipset_power_state *ps;
215 static uint32_t gpe0_sts[GPE0_REG_MAX];
216 uint32_t pm1_en;
217 int i;
218
219 ps = cbmem_find(CBMEM_ID_POWER_STATE);
220 if (ps == NULL)
221 return -1;
222
223 /*
224 * PM1_EN to check the basic wake events which can happen through
225 * powerbtn or any other wake source like lidopen, key board press etc.
226 */
227 pm1_en = ps->pm1_en;
228
229 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
230
231 *pm1 = ps->pm1_sts & pm1_en;
232
233 /* Mask off GPE0 status bits that are not enabled */
234 *gpe0 = &gpe0_sts[0];
235 for (i = 0; i < GPE0_REG_MAX; i++)
236 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
237
238 return GPE0_REG_MAX;
239}
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200240#endif
Shaunak Sahabd427802017-07-18 00:19:33 -0700241
Aaron Durbin64031672018-04-21 14:45:32 -0600242__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700243{
244}
245
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200246void southbridge_inject_dsdt(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700247{
248 struct global_nvs_t *gnvs;
249
250 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
251 if (!gnvs) {
252 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
253 if (gnvs)
254 memset(gnvs, 0, sizeof(*gnvs));
255 }
256
257 if (gnvs) {
258 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700259 /* And tell SMI about it */
260 smm_setup_structures(gnvs, NULL, NULL);
261
262 /* Add it to DSDT. */
263 acpigen_write_scope("\\");
264 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
265 acpigen_pop_len();
266 }
267}
268
269static int calculate_power(int tdp, int p1_ratio, int ratio)
270{
271 u32 m;
272 u32 power;
273
274 /*
275 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
276 *
277 * Power = (ratio / p1_ratio) * m * tdp
278 */
279
280 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
281 m = (m * m) / 1000;
282
283 power = ((ratio * 100000 / p1_ratio) / 100);
284 power *= (m / 100) * (tdp / 1000);
285 power /= 1000;
286
287 return power;
288}
289
290static int get_cores_per_package(void)
291{
292 struct cpuinfo_x86 c;
293 struct cpuid_result result;
294 int cores = 1;
295
296 get_fms(&c, cpuid_eax(1));
297 if (c.x86 != 6)
298 return 1;
299
300 result = cpuid_ext(0xb, 1);
301 cores = result.ebx & 0xff;
302
303 return cores;
304}
305
306static void generate_c_state_entries(void)
307{
308 acpi_cstate_t *c_state_map;
309 size_t entries;
310
311 c_state_map = soc_get_cstate_map(&entries);
312
313 /* Generate C-state tables */
314 acpigen_write_CST_package(c_state_map, entries);
315}
316
317void generate_p_state_entries(int core, int cores_per_package)
318{
319 int ratio_min, ratio_max, ratio_turbo, ratio_step;
320 int coord_type, power_max, num_entries;
321 int ratio, power, clock, clock_max;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100322 bool turbo;
Shaunak Sahabd427802017-07-18 00:19:33 -0700323
324 coord_type = cpu_get_coord_type();
325 ratio_min = cpu_get_min_ratio();
326 ratio_max = cpu_get_max_ratio();
327 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100328 turbo = (get_turbo_state() == TURBO_ENABLED);
Shaunak Sahabd427802017-07-18 00:19:33 -0700329
330 /* Calculate CPU TDP in mW */
331 power_max = cpu_get_power_max();
332
333 /* Write _PCT indicating use of FFixedHW */
334 acpigen_write_empty_PCT();
335
336 /* Write _PPC with no limit on supported P-state */
337 acpigen_write_PPC_NVS();
338 /* Write PSD indicating configured coordination type */
339 acpigen_write_PSD_package(core, 1, coord_type);
340
341 /* Add P-state entries in _PSS table */
342 acpigen_write_name("_PSS");
343
344 /* Determine ratio points */
345 ratio_step = PSS_RATIO_STEP;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100346 do {
Shaunak Sahabd427802017-07-18 00:19:33 -0700347 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100348 if (((ratio_max - ratio_min) % ratio_step) > 0)
349 num_entries += 1;
350 if (turbo)
351 num_entries += 1;
352 if (num_entries > PSS_MAX_ENTRIES)
353 ratio_step += 1;
354 } while (num_entries > PSS_MAX_ENTRIES);
355
356 /* _PSS package count depends on Turbo */
357 acpigen_write_package(num_entries);
Shaunak Sahabd427802017-07-18 00:19:33 -0700358
359 /* P[T] is Turbo state if enabled */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100360 if (turbo) {
Shaunak Sahabd427802017-07-18 00:19:33 -0700361 ratio_turbo = cpu_get_max_turbo_ratio();
362
363 /* Add entry for Turbo ratio */
364 acpigen_write_PSS_package(clock_max + 1, /* MHz */
365 power_max, /* mW */
366 PSS_LATENCY_TRANSITION,/* lat1 */
367 PSS_LATENCY_BUSMASTER,/* lat2 */
368 ratio_turbo << 8, /* control */
369 ratio_turbo << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100370 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700371 }
372
373 /* First regular entry is max non-turbo ratio */
374 acpigen_write_PSS_package(clock_max, /* MHz */
375 power_max, /* mW */
376 PSS_LATENCY_TRANSITION,/* lat1 */
377 PSS_LATENCY_BUSMASTER,/* lat2 */
378 ratio_max << 8, /* control */
379 ratio_max << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100380 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700381
382 /* Generate the remaining entries */
383 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
384 ratio >= ratio_min; ratio -= ratio_step) {
385
386 /* Calculate power at this ratio */
387 power = calculate_power(power_max, ratio_max, ratio);
388 clock = (ratio * cpu_get_bus_clock()) / KHz;
389
390 acpigen_write_PSS_package(clock, /* MHz */
391 power, /* mW */
392 PSS_LATENCY_TRANSITION,/* lat1 */
393 PSS_LATENCY_BUSMASTER,/* lat2 */
394 ratio << 8, /* control */
395 ratio << 8); /* status */
396 }
397 /* Fix package length */
398 acpigen_pop_len();
399}
400
Julien Viard de Galbert595202c2018-03-29 14:01:01 +0200401__attribute__ ((weak)) acpi_tstate_t *soc_get_tss_table(int *entries)
Shaunak Sahabd427802017-07-18 00:19:33 -0700402{
403 *entries = 0;
404 return NULL;
405}
406
407void generate_t_state_entries(int core, int cores_per_package)
408{
409 acpi_tstate_t *soc_tss_table;
410 int entries;
411
412 soc_tss_table = soc_get_tss_table(&entries);
413 if (entries == 0)
414 return;
415
416 /* Indicate SW_ALL coordination for T-states */
417 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
418
419 /* Indicate FixedHW so OS will use MSR */
420 acpigen_write_empty_PTC();
421
422 /* Set NVS controlled T-state limit */
423 acpigen_write_TPC("\\TLVL");
424
425 /* Write TSS table for MSR access */
426 acpigen_write_TSS_package(entries, soc_tss_table);
427}
428
Aaron Durbin64031672018-04-21 14:45:32 -0600429__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700430 int cores_per_package)
431{
432}
433
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200434void generate_cpu_entries(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700435{
436 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
437 int plen = 6;
438 int totalcores = dev_count_cpu();
439 int cores_per_package = get_cores_per_package();
440 int numcpus = totalcores / cores_per_package;
441
442 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
443 numcpus, cores_per_package);
444
445 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
446 for (core_id = 0; core_id < cores_per_package; core_id++) {
447 if (core_id > 0) {
448 pcontrol_blk = 0;
449 plen = 0;
450 }
451
Christian Walterbe3979c2019-12-18 15:07:59 +0100452 /* Generate processor \_SB.CPUx */
Shaunak Sahabd427802017-07-18 00:19:33 -0700453 acpigen_write_processor((cpu_id) * cores_per_package +
454 core_id, pcontrol_blk, plen);
455
456 /* Generate C-state tables */
457 generate_c_state_entries();
458
459 /* Soc specific power states generation */
460 soc_power_states_generation(core_id, cores_per_package);
461
462 acpigen_pop_len();
463 }
464 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100465 /* PPKG is usually used for thermal management
466 of the first and only package. */
467 acpigen_write_processor_package("PPKG", 0, cores_per_package);
468
469 /* Add a method to notify processor nodes */
470 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700471}
472
Julius Wernercd49cce2019-03-05 16:53:33 -0800473#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700474/* Save wake source data for ACPI _SWS methods in NVS */
475static void acpi_save_wake_source(void *unused)
476{
477 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
478 uint32_t pm1, *gpe0;
479 int gpe_reg, gpe_reg_count;
480 int reg_size = sizeof(uint32_t) * 8;
481
482 if (!gnvs)
483 return;
484
485 gnvs->pm1i = -1;
486 gnvs->gpei = -1;
487
488 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
489 if (gpe_reg_count < 0)
490 return;
491
492 /* Scan for first set bit in PM1 */
493 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
494 if (pm1 & 1)
495 break;
496 pm1 >>= 1;
497 }
498
499 /* If unable to determine then return -1 */
500 if (gnvs->pm1i >= 16)
501 gnvs->pm1i = -1;
502
503 /* Scan for first set bit in GPE registers */
504 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
505 uint32_t gpe = gpe0[gpe_reg];
506 int start = gpe_reg * reg_size;
507 int end = start + reg_size;
508
509 if (gpe == 0) {
510 if (!gnvs->gpei)
511 gnvs->gpei = end;
512 continue;
513 }
514
515 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
516 if (gpe & 1)
517 break;
518 gpe >>= 1;
519 }
520 }
521
522 /* If unable to determine then return -1 */
523 if (gnvs->gpei >= gpe_reg_count * reg_size)
524 gnvs->gpei = -1;
525
526 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
527 (long long)gnvs->pm1i, (long long)gnvs->gpei);
528}
529
530BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
531
532#endif