blob: 3a34c79304f634013fe2a5b37e8f9d22b7af7b5b [file] [log] [blame]
Shaunak Sahabd427802017-07-18 00:19:33 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 Intel Corp.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010016
Shaunak Sahabd427802017-07-18 00:19:33 -070017#include <arch/acpigen.h>
18#include <arch/ioapic.h>
19#include <arch/smp/mpspec.h>
20#include <bootstate.h>
21#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +020022#include <cf9_reset.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +010023#include <console/console.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070024#include <cpu/intel/turbo.h>
25#include <cpu/x86/msr.h>
26#include <cpu/x86/smm.h>
27#include <intelblocks/acpi.h>
28#include <intelblocks/msr.h>
29#include <intelblocks/pmclib.h>
Duncan Laurie93bbd412017-11-11 20:03:29 -080030#include <intelblocks/uart.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070031#include <soc/gpio.h>
32#include <soc/iomap.h>
33#include <soc/nvs.h>
34#include <soc/pm.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010035#include <string.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070036
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +020037__attribute__((weak)) unsigned long acpi_fill_mcfg(unsigned long current)
Shaunak Sahabd427802017-07-18 00:19:33 -070038{
39 /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */
40 current += acpi_create_mcfg_mmconfig((void *)current,
41 CONFIG_MMCONF_BASE_ADDRESS, 0, 0,
Duncan Lauriefd50b7c2018-03-02 14:47:11 -080042 (CONFIG_SA_PCIEX_LENGTH >> 20) - 1);
Shaunak Sahabd427802017-07-18 00:19:33 -070043 return current;
44}
45
46static int acpi_sci_irq(void)
47{
48 int sci_irq = 9;
49 uint32_t scis;
50
51 scis = soc_read_sci_irq_select();
52 scis &= SCI_IRQ_SEL;
53 scis >>= SCI_IRQ_ADJUST;
54
55 /* Determine how SCI is routed. */
56 switch (scis) {
57 case SCIS_IRQ9:
58 case SCIS_IRQ10:
59 case SCIS_IRQ11:
60 sci_irq = scis - SCIS_IRQ9 + 9;
61 break;
62 case SCIS_IRQ20:
63 case SCIS_IRQ21:
64 case SCIS_IRQ22:
65 case SCIS_IRQ23:
66 sci_irq = scis - SCIS_IRQ20 + 20;
67 break;
68 default:
69 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
70 sci_irq = 9;
71 break;
72 }
73
74 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
75 return sci_irq;
76}
77
78static unsigned long acpi_madt_irq_overrides(unsigned long current)
79{
80 int sci = acpi_sci_irq();
81 uint16_t flags = MP_IRQ_TRIGGER_LEVEL;
82
83 /* INT_SRC_OVR */
84 current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0);
85
86 flags |= soc_madt_sci_irq_polarity(sci);
87
88 /* SCI */
89 current +=
90 acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags);
91
92 return current;
93}
94
95unsigned long acpi_fill_madt(unsigned long current)
96{
97 /* Local APICs */
98 current = acpi_create_madt_lapics(current);
99
100 /* IOAPIC */
101 current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0);
102
103 return acpi_madt_irq_overrides(current);
104}
105
Aaron Durbin64031672018-04-21 14:45:32 -0600106__weak void soc_fill_fadt(acpi_fadt_t *fadt)
Shaunak Sahabd427802017-07-18 00:19:33 -0700107{
108}
109
110void acpi_fill_fadt(acpi_fadt_t *fadt)
111{
112 const uint16_t pmbase = ACPI_BASE_ADDRESS;
113
114 /* Use ACPI 3.0 revision. */
Marc Jonesf9ea7ed2018-08-22 18:59:26 -0600115 fadt->header.revision = get_acpi_table_revision(FADT);
Shaunak Sahabd427802017-07-18 00:19:33 -0700116
117 fadt->sci_int = acpi_sci_irq();
118 fadt->smi_cmd = APM_CNT;
119 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
120 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
121 fadt->s4bios_req = 0x0;
122 fadt->pstate_cnt = 0;
123
124 fadt->pm1a_evt_blk = pmbase + PM1_STS;
125 fadt->pm1b_evt_blk = 0x0;
126 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
127 fadt->pm1b_cnt_blk = 0x0;
128
129 fadt->gpe0_blk = pmbase + GPE0_STS(0);
130
131 fadt->pm1_evt_len = 4;
132 fadt->pm1_cnt_len = 2;
133
134 /* GPE0 STS/EN pairs each 32 bits wide. */
135 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
136
137 fadt->flush_size = 0x400; /* twice of cache size */
138 fadt->flush_stride = 0x10; /* Cache line width */
139 fadt->duty_offset = 1;
140 fadt->day_alrm = 0xd;
141
142 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
143 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
144 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
145 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
146
147 fadt->reset_reg.space_id = 1;
148 fadt->reset_reg.bit_width = 8;
149 fadt->reset_reg.addrl = RST_CNT;
150 fadt->reset_value = RST_CPU | SYS_RST;
151
152 fadt->x_pm1a_evt_blk.space_id = 1;
153 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
154 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
155
156 fadt->x_pm1b_evt_blk.space_id = 1;
157
158 fadt->x_pm1a_cnt_blk.space_id = 1;
159 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
160 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
161
162 fadt->x_pm1b_cnt_blk.space_id = 1;
163
164 fadt->x_gpe1_blk.space_id = 1;
165
166 soc_fill_fadt(fadt);
167}
168
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200169unsigned long southbridge_write_acpi_tables(struct device *device,
Shaunak Sahabd427802017-07-18 00:19:33 -0700170 unsigned long current,
171 struct acpi_rsdp *rsdp)
172{
Duncan Laurie93bbd412017-11-11 20:03:29 -0800173 current = acpi_write_dbg2_pci_uart(rsdp, current,
Subrata Banikafa07f72018-05-24 12:21:06 +0530174 uart_get_device(),
Duncan Laurie93bbd412017-11-11 20:03:29 -0800175 ACPI_ACCESS_SIZE_DWORD_ACCESS);
Shaunak Sahabd427802017-07-18 00:19:33 -0700176 return acpi_write_hpet(device, current, rsdp);
177}
178
Aaron Durbin64031672018-04-21 14:45:32 -0600179__weak
Shaunak Sahabd427802017-07-18 00:19:33 -0700180uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
181 const struct chipset_power_state *ps)
182{
183 return generic_pm1_en;
184}
185
Julius Wernercd49cce2019-03-05 16:53:33 -0800186#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700187/*
188 * Save wake source information for calculating ACPI _SWS values
189 *
190 * @pm1: PM1_STS register with only enabled events set
191 * @gpe0: GPE0_STS registers with only enabled events set
192 *
193 * return the number of registers in the gpe0 array or -1 if nothing
194 * is provided by this function.
195 */
196
197static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
198{
199 struct chipset_power_state *ps;
200 static uint32_t gpe0_sts[GPE0_REG_MAX];
201 uint32_t pm1_en;
202 int i;
203
204 ps = cbmem_find(CBMEM_ID_POWER_STATE);
205 if (ps == NULL)
206 return -1;
207
208 /*
209 * PM1_EN to check the basic wake events which can happen through
210 * powerbtn or any other wake source like lidopen, key board press etc.
211 */
212 pm1_en = ps->pm1_en;
213
214 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
215
216 *pm1 = ps->pm1_sts & pm1_en;
217
218 /* Mask off GPE0 status bits that are not enabled */
219 *gpe0 = &gpe0_sts[0];
220 for (i = 0; i < GPE0_REG_MAX; i++)
221 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
222
223 return GPE0_REG_MAX;
224}
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200225#endif
Shaunak Sahabd427802017-07-18 00:19:33 -0700226
Aaron Durbin64031672018-04-21 14:45:32 -0600227__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700228{
229}
230
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200231void southbridge_inject_dsdt(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700232{
233 struct global_nvs_t *gnvs;
234
235 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
236 if (!gnvs) {
237 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
238 if (gnvs)
239 memset(gnvs, 0, sizeof(*gnvs));
240 }
241
242 if (gnvs) {
243 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700244 /* And tell SMI about it */
245 smm_setup_structures(gnvs, NULL, NULL);
246
247 /* Add it to DSDT. */
248 acpigen_write_scope("\\");
249 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
250 acpigen_pop_len();
251 }
252}
253
254static int calculate_power(int tdp, int p1_ratio, int ratio)
255{
256 u32 m;
257 u32 power;
258
259 /*
260 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
261 *
262 * Power = (ratio / p1_ratio) * m * tdp
263 */
264
265 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
266 m = (m * m) / 1000;
267
268 power = ((ratio * 100000 / p1_ratio) / 100);
269 power *= (m / 100) * (tdp / 1000);
270 power /= 1000;
271
272 return power;
273}
274
275static int get_cores_per_package(void)
276{
277 struct cpuinfo_x86 c;
278 struct cpuid_result result;
279 int cores = 1;
280
281 get_fms(&c, cpuid_eax(1));
282 if (c.x86 != 6)
283 return 1;
284
285 result = cpuid_ext(0xb, 1);
286 cores = result.ebx & 0xff;
287
288 return cores;
289}
290
291static void generate_c_state_entries(void)
292{
293 acpi_cstate_t *c_state_map;
294 size_t entries;
295
296 c_state_map = soc_get_cstate_map(&entries);
297
298 /* Generate C-state tables */
299 acpigen_write_CST_package(c_state_map, entries);
300}
301
302void generate_p_state_entries(int core, int cores_per_package)
303{
304 int ratio_min, ratio_max, ratio_turbo, ratio_step;
305 int coord_type, power_max, num_entries;
306 int ratio, power, clock, clock_max;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100307 bool turbo;
Shaunak Sahabd427802017-07-18 00:19:33 -0700308
309 coord_type = cpu_get_coord_type();
310 ratio_min = cpu_get_min_ratio();
311 ratio_max = cpu_get_max_ratio();
312 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100313 turbo = (get_turbo_state() == TURBO_ENABLED);
Shaunak Sahabd427802017-07-18 00:19:33 -0700314
315 /* Calculate CPU TDP in mW */
316 power_max = cpu_get_power_max();
317
318 /* Write _PCT indicating use of FFixedHW */
319 acpigen_write_empty_PCT();
320
321 /* Write _PPC with no limit on supported P-state */
322 acpigen_write_PPC_NVS();
323 /* Write PSD indicating configured coordination type */
324 acpigen_write_PSD_package(core, 1, coord_type);
325
326 /* Add P-state entries in _PSS table */
327 acpigen_write_name("_PSS");
328
329 /* Determine ratio points */
330 ratio_step = PSS_RATIO_STEP;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100331 do {
Shaunak Sahabd427802017-07-18 00:19:33 -0700332 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100333 if (((ratio_max - ratio_min) % ratio_step) > 0)
334 num_entries += 1;
335 if (turbo)
336 num_entries += 1;
337 if (num_entries > PSS_MAX_ENTRIES)
338 ratio_step += 1;
339 } while (num_entries > PSS_MAX_ENTRIES);
340
341 /* _PSS package count depends on Turbo */
342 acpigen_write_package(num_entries);
Shaunak Sahabd427802017-07-18 00:19:33 -0700343
344 /* P[T] is Turbo state if enabled */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100345 if (turbo) {
Shaunak Sahabd427802017-07-18 00:19:33 -0700346 ratio_turbo = cpu_get_max_turbo_ratio();
347
348 /* Add entry for Turbo ratio */
349 acpigen_write_PSS_package(clock_max + 1, /* MHz */
350 power_max, /* mW */
351 PSS_LATENCY_TRANSITION,/* lat1 */
352 PSS_LATENCY_BUSMASTER,/* lat2 */
353 ratio_turbo << 8, /* control */
354 ratio_turbo << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100355 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700356 }
357
358 /* First regular entry is max non-turbo ratio */
359 acpigen_write_PSS_package(clock_max, /* MHz */
360 power_max, /* mW */
361 PSS_LATENCY_TRANSITION,/* lat1 */
362 PSS_LATENCY_BUSMASTER,/* lat2 */
363 ratio_max << 8, /* control */
364 ratio_max << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100365 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700366
367 /* Generate the remaining entries */
368 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
369 ratio >= ratio_min; ratio -= ratio_step) {
370
371 /* Calculate power at this ratio */
372 power = calculate_power(power_max, ratio_max, ratio);
373 clock = (ratio * cpu_get_bus_clock()) / KHz;
374
375 acpigen_write_PSS_package(clock, /* MHz */
376 power, /* mW */
377 PSS_LATENCY_TRANSITION,/* lat1 */
378 PSS_LATENCY_BUSMASTER,/* lat2 */
379 ratio << 8, /* control */
380 ratio << 8); /* status */
381 }
382 /* Fix package length */
383 acpigen_pop_len();
384}
385
Julien Viard de Galbert595202c2018-03-29 14:01:01 +0200386__attribute__ ((weak)) acpi_tstate_t *soc_get_tss_table(int *entries)
Shaunak Sahabd427802017-07-18 00:19:33 -0700387{
388 *entries = 0;
389 return NULL;
390}
391
392void generate_t_state_entries(int core, int cores_per_package)
393{
394 acpi_tstate_t *soc_tss_table;
395 int entries;
396
397 soc_tss_table = soc_get_tss_table(&entries);
398 if (entries == 0)
399 return;
400
401 /* Indicate SW_ALL coordination for T-states */
402 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
403
404 /* Indicate FixedHW so OS will use MSR */
405 acpigen_write_empty_PTC();
406
407 /* Set NVS controlled T-state limit */
408 acpigen_write_TPC("\\TLVL");
409
410 /* Write TSS table for MSR access */
411 acpigen_write_TSS_package(entries, soc_tss_table);
412}
413
Aaron Durbin64031672018-04-21 14:45:32 -0600414__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700415 int cores_per_package)
416{
417}
418
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200419void generate_cpu_entries(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700420{
421 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
422 int plen = 6;
423 int totalcores = dev_count_cpu();
424 int cores_per_package = get_cores_per_package();
425 int numcpus = totalcores / cores_per_package;
426
427 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
428 numcpus, cores_per_package);
429
430 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
431 for (core_id = 0; core_id < cores_per_package; core_id++) {
432 if (core_id > 0) {
433 pcontrol_blk = 0;
434 plen = 0;
435 }
436
437 /* Generate processor \_PR.CPUx */
438 acpigen_write_processor((cpu_id) * cores_per_package +
439 core_id, pcontrol_blk, plen);
440
441 /* Generate C-state tables */
442 generate_c_state_entries();
443
444 /* Soc specific power states generation */
445 soc_power_states_generation(core_id, cores_per_package);
446
447 acpigen_pop_len();
448 }
449 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100450 /* PPKG is usually used for thermal management
451 of the first and only package. */
452 acpigen_write_processor_package("PPKG", 0, cores_per_package);
453
454 /* Add a method to notify processor nodes */
455 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700456}
457
Julius Wernercd49cce2019-03-05 16:53:33 -0800458#if CONFIG(SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700459/* Save wake source data for ACPI _SWS methods in NVS */
460static void acpi_save_wake_source(void *unused)
461{
462 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
463 uint32_t pm1, *gpe0;
464 int gpe_reg, gpe_reg_count;
465 int reg_size = sizeof(uint32_t) * 8;
466
467 if (!gnvs)
468 return;
469
470 gnvs->pm1i = -1;
471 gnvs->gpei = -1;
472
473 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
474 if (gpe_reg_count < 0)
475 return;
476
477 /* Scan for first set bit in PM1 */
478 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
479 if (pm1 & 1)
480 break;
481 pm1 >>= 1;
482 }
483
484 /* If unable to determine then return -1 */
485 if (gnvs->pm1i >= 16)
486 gnvs->pm1i = -1;
487
488 /* Scan for first set bit in GPE registers */
489 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
490 uint32_t gpe = gpe0[gpe_reg];
491 int start = gpe_reg * reg_size;
492 int end = start + reg_size;
493
494 if (gpe == 0) {
495 if (!gnvs->gpei)
496 gnvs->gpei = end;
497 continue;
498 }
499
500 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
501 if (gpe & 1)
502 break;
503 gpe >>= 1;
504 }
505 }
506
507 /* If unable to determine then return -1 */
508 if (gnvs->gpei >= gpe_reg_count * reg_size)
509 gnvs->gpei = -1;
510
511 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
512 (long long)gnvs->pm1i, (long long)gnvs->gpei);
513}
514
515BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
516
517#endif