blob: 002774400342a7529f700e4649533ca3d77a6a34 [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 */
16#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>
Shaunak Sahabd427802017-07-18 00:19:33 -070022#include <cpu/intel/turbo.h>
23#include <cpu/x86/msr.h>
24#include <cpu/x86/smm.h>
25#include <intelblocks/acpi.h>
26#include <intelblocks/msr.h>
27#include <intelblocks/pmclib.h>
Duncan Laurie93bbd412017-11-11 20:03:29 -080028#include <intelblocks/uart.h>
Shaunak Sahabd427802017-07-18 00:19:33 -070029#include <soc/gpio.h>
30#include <soc/iomap.h>
31#include <soc/nvs.h>
32#include <soc/pm.h>
33
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +020034__attribute__((weak)) unsigned long acpi_fill_mcfg(unsigned long current)
Shaunak Sahabd427802017-07-18 00:19:33 -070035{
36 /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */
37 current += acpi_create_mcfg_mmconfig((void *)current,
38 CONFIG_MMCONF_BASE_ADDRESS, 0, 0,
Duncan Lauriefd50b7c2018-03-02 14:47:11 -080039 (CONFIG_SA_PCIEX_LENGTH >> 20) - 1);
Shaunak Sahabd427802017-07-18 00:19:33 -070040 return current;
41}
42
43static int acpi_sci_irq(void)
44{
45 int sci_irq = 9;
46 uint32_t scis;
47
48 scis = soc_read_sci_irq_select();
49 scis &= SCI_IRQ_SEL;
50 scis >>= SCI_IRQ_ADJUST;
51
52 /* Determine how SCI is routed. */
53 switch (scis) {
54 case SCIS_IRQ9:
55 case SCIS_IRQ10:
56 case SCIS_IRQ11:
57 sci_irq = scis - SCIS_IRQ9 + 9;
58 break;
59 case SCIS_IRQ20:
60 case SCIS_IRQ21:
61 case SCIS_IRQ22:
62 case SCIS_IRQ23:
63 sci_irq = scis - SCIS_IRQ20 + 20;
64 break;
65 default:
66 printk(BIOS_DEBUG, "Invalid SCI route! Defaulting to IRQ9.\n");
67 sci_irq = 9;
68 break;
69 }
70
71 printk(BIOS_DEBUG, "SCI is IRQ%d\n", sci_irq);
72 return sci_irq;
73}
74
75static unsigned long acpi_madt_irq_overrides(unsigned long current)
76{
77 int sci = acpi_sci_irq();
78 uint16_t flags = MP_IRQ_TRIGGER_LEVEL;
79
80 /* INT_SRC_OVR */
81 current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0);
82
83 flags |= soc_madt_sci_irq_polarity(sci);
84
85 /* SCI */
86 current +=
87 acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags);
88
89 return current;
90}
91
92unsigned long acpi_fill_madt(unsigned long current)
93{
94 /* Local APICs */
95 current = acpi_create_madt_lapics(current);
96
97 /* IOAPIC */
98 current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0);
99
100 return acpi_madt_irq_overrides(current);
101}
102
Aaron Durbin64031672018-04-21 14:45:32 -0600103__weak void soc_fill_fadt(acpi_fadt_t *fadt)
Shaunak Sahabd427802017-07-18 00:19:33 -0700104{
105}
106
107void acpi_fill_fadt(acpi_fadt_t *fadt)
108{
109 const uint16_t pmbase = ACPI_BASE_ADDRESS;
110
111 /* Use ACPI 3.0 revision. */
Marc Jonesf9ea7ed2018-08-22 18:59:26 -0600112 fadt->header.revision = get_acpi_table_revision(FADT);
Shaunak Sahabd427802017-07-18 00:19:33 -0700113
114 fadt->sci_int = acpi_sci_irq();
115 fadt->smi_cmd = APM_CNT;
116 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
117 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
118 fadt->s4bios_req = 0x0;
119 fadt->pstate_cnt = 0;
120
121 fadt->pm1a_evt_blk = pmbase + PM1_STS;
122 fadt->pm1b_evt_blk = 0x0;
123 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
124 fadt->pm1b_cnt_blk = 0x0;
125
126 fadt->gpe0_blk = pmbase + GPE0_STS(0);
127
128 fadt->pm1_evt_len = 4;
129 fadt->pm1_cnt_len = 2;
130
131 /* GPE0 STS/EN pairs each 32 bits wide. */
132 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
133
134 fadt->flush_size = 0x400; /* twice of cache size */
135 fadt->flush_stride = 0x10; /* Cache line width */
136 fadt->duty_offset = 1;
137 fadt->day_alrm = 0xd;
138
139 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
140 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
141 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
142 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
143
144 fadt->reset_reg.space_id = 1;
145 fadt->reset_reg.bit_width = 8;
146 fadt->reset_reg.addrl = RST_CNT;
147 fadt->reset_value = RST_CPU | SYS_RST;
148
149 fadt->x_pm1a_evt_blk.space_id = 1;
150 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
151 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
152
153 fadt->x_pm1b_evt_blk.space_id = 1;
154
155 fadt->x_pm1a_cnt_blk.space_id = 1;
156 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
157 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
158
159 fadt->x_pm1b_cnt_blk.space_id = 1;
160
161 fadt->x_gpe1_blk.space_id = 1;
162
163 soc_fill_fadt(fadt);
164}
165
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200166unsigned long southbridge_write_acpi_tables(struct device *device,
Shaunak Sahabd427802017-07-18 00:19:33 -0700167 unsigned long current,
168 struct acpi_rsdp *rsdp)
169{
Duncan Laurie93bbd412017-11-11 20:03:29 -0800170 current = acpi_write_dbg2_pci_uart(rsdp, current,
Subrata Banikafa07f72018-05-24 12:21:06 +0530171 uart_get_device(),
Duncan Laurie93bbd412017-11-11 20:03:29 -0800172 ACPI_ACCESS_SIZE_DWORD_ACCESS);
Shaunak Sahabd427802017-07-18 00:19:33 -0700173 return acpi_write_hpet(device, current, rsdp);
174}
175
Aaron Durbin64031672018-04-21 14:45:32 -0600176__weak
Shaunak Sahabd427802017-07-18 00:19:33 -0700177uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
178 const struct chipset_power_state *ps)
179{
180 return generic_pm1_en;
181}
182
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200183#if IS_ENABLED(CONFIG_SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
Shaunak Sahabd427802017-07-18 00:19:33 -0700184/*
185 * Save wake source information for calculating ACPI _SWS values
186 *
187 * @pm1: PM1_STS register with only enabled events set
188 * @gpe0: GPE0_STS registers with only enabled events set
189 *
190 * return the number of registers in the gpe0 array or -1 if nothing
191 * is provided by this function.
192 */
193
194static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
195{
196 struct chipset_power_state *ps;
197 static uint32_t gpe0_sts[GPE0_REG_MAX];
198 uint32_t pm1_en;
199 int i;
200
201 ps = cbmem_find(CBMEM_ID_POWER_STATE);
202 if (ps == NULL)
203 return -1;
204
205 /*
206 * PM1_EN to check the basic wake events which can happen through
207 * powerbtn or any other wake source like lidopen, key board press etc.
208 */
209 pm1_en = ps->pm1_en;
210
211 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
212
213 *pm1 = ps->pm1_sts & pm1_en;
214
215 /* Mask off GPE0 status bits that are not enabled */
216 *gpe0 = &gpe0_sts[0];
217 for (i = 0; i < GPE0_REG_MAX; i++)
218 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
219
220 return GPE0_REG_MAX;
221}
Julien Viard de Galbertcf2b72f2018-04-05 11:24:45 +0200222#endif
Shaunak Sahabd427802017-07-18 00:19:33 -0700223
Aaron Durbin64031672018-04-21 14:45:32 -0600224__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700225{
226}
227
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200228void southbridge_inject_dsdt(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700229{
230 struct global_nvs_t *gnvs;
231
232 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
233 if (!gnvs) {
234 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
235 if (gnvs)
236 memset(gnvs, 0, sizeof(*gnvs));
237 }
238
239 if (gnvs) {
240 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700241 /* And tell SMI about it */
242 smm_setup_structures(gnvs, NULL, NULL);
243
244 /* Add it to DSDT. */
245 acpigen_write_scope("\\");
246 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
247 acpigen_pop_len();
248 }
249}
250
251static int calculate_power(int tdp, int p1_ratio, int ratio)
252{
253 u32 m;
254 u32 power;
255
256 /*
257 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
258 *
259 * Power = (ratio / p1_ratio) * m * tdp
260 */
261
262 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
263 m = (m * m) / 1000;
264
265 power = ((ratio * 100000 / p1_ratio) / 100);
266 power *= (m / 100) * (tdp / 1000);
267 power /= 1000;
268
269 return power;
270}
271
272static int get_cores_per_package(void)
273{
274 struct cpuinfo_x86 c;
275 struct cpuid_result result;
276 int cores = 1;
277
278 get_fms(&c, cpuid_eax(1));
279 if (c.x86 != 6)
280 return 1;
281
282 result = cpuid_ext(0xb, 1);
283 cores = result.ebx & 0xff;
284
285 return cores;
286}
287
288static void generate_c_state_entries(void)
289{
290 acpi_cstate_t *c_state_map;
291 size_t entries;
292
293 c_state_map = soc_get_cstate_map(&entries);
294
295 /* Generate C-state tables */
296 acpigen_write_CST_package(c_state_map, entries);
297}
298
299void generate_p_state_entries(int core, int cores_per_package)
300{
301 int ratio_min, ratio_max, ratio_turbo, ratio_step;
302 int coord_type, power_max, num_entries;
303 int ratio, power, clock, clock_max;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100304 bool turbo;
Shaunak Sahabd427802017-07-18 00:19:33 -0700305
306 coord_type = cpu_get_coord_type();
307 ratio_min = cpu_get_min_ratio();
308 ratio_max = cpu_get_max_ratio();
309 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100310 turbo = (get_turbo_state() == TURBO_ENABLED);
Shaunak Sahabd427802017-07-18 00:19:33 -0700311
312 /* Calculate CPU TDP in mW */
313 power_max = cpu_get_power_max();
314
315 /* Write _PCT indicating use of FFixedHW */
316 acpigen_write_empty_PCT();
317
318 /* Write _PPC with no limit on supported P-state */
319 acpigen_write_PPC_NVS();
320 /* Write PSD indicating configured coordination type */
321 acpigen_write_PSD_package(core, 1, coord_type);
322
323 /* Add P-state entries in _PSS table */
324 acpigen_write_name("_PSS");
325
326 /* Determine ratio points */
327 ratio_step = PSS_RATIO_STEP;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100328 do {
Shaunak Sahabd427802017-07-18 00:19:33 -0700329 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100330 if (((ratio_max - ratio_min) % ratio_step) > 0)
331 num_entries += 1;
332 if (turbo)
333 num_entries += 1;
334 if (num_entries > PSS_MAX_ENTRIES)
335 ratio_step += 1;
336 } while (num_entries > PSS_MAX_ENTRIES);
337
338 /* _PSS package count depends on Turbo */
339 acpigen_write_package(num_entries);
Shaunak Sahabd427802017-07-18 00:19:33 -0700340
341 /* P[T] is Turbo state if enabled */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100342 if (turbo) {
Shaunak Sahabd427802017-07-18 00:19:33 -0700343 ratio_turbo = cpu_get_max_turbo_ratio();
344
345 /* Add entry for Turbo ratio */
346 acpigen_write_PSS_package(clock_max + 1, /* MHz */
347 power_max, /* mW */
348 PSS_LATENCY_TRANSITION,/* lat1 */
349 PSS_LATENCY_BUSMASTER,/* lat2 */
350 ratio_turbo << 8, /* control */
351 ratio_turbo << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100352 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700353 }
354
355 /* First regular entry is max non-turbo ratio */
356 acpigen_write_PSS_package(clock_max, /* MHz */
357 power_max, /* mW */
358 PSS_LATENCY_TRANSITION,/* lat1 */
359 PSS_LATENCY_BUSMASTER,/* lat2 */
360 ratio_max << 8, /* control */
361 ratio_max << 8); /* status */
Julien Viard de Galbertc2540a92018-11-06 09:28:03 +0100362 num_entries -= 1;
Shaunak Sahabd427802017-07-18 00:19:33 -0700363
364 /* Generate the remaining entries */
365 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
366 ratio >= ratio_min; ratio -= ratio_step) {
367
368 /* Calculate power at this ratio */
369 power = calculate_power(power_max, ratio_max, ratio);
370 clock = (ratio * cpu_get_bus_clock()) / KHz;
371
372 acpigen_write_PSS_package(clock, /* MHz */
373 power, /* mW */
374 PSS_LATENCY_TRANSITION,/* lat1 */
375 PSS_LATENCY_BUSMASTER,/* lat2 */
376 ratio << 8, /* control */
377 ratio << 8); /* status */
378 }
379 /* Fix package length */
380 acpigen_pop_len();
381}
382
Julien Viard de Galbert595202c2018-03-29 14:01:01 +0200383__attribute__ ((weak)) acpi_tstate_t *soc_get_tss_table(int *entries)
Shaunak Sahabd427802017-07-18 00:19:33 -0700384{
385 *entries = 0;
386 return NULL;
387}
388
389void generate_t_state_entries(int core, int cores_per_package)
390{
391 acpi_tstate_t *soc_tss_table;
392 int entries;
393
394 soc_tss_table = soc_get_tss_table(&entries);
395 if (entries == 0)
396 return;
397
398 /* Indicate SW_ALL coordination for T-states */
399 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
400
401 /* Indicate FixedHW so OS will use MSR */
402 acpigen_write_empty_PTC();
403
404 /* Set NVS controlled T-state limit */
405 acpigen_write_TPC("\\TLVL");
406
407 /* Write TSS table for MSR access */
408 acpigen_write_TSS_package(entries, soc_tss_table);
409}
410
Aaron Durbin64031672018-04-21 14:45:32 -0600411__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700412 int cores_per_package)
413{
414}
415
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200416void generate_cpu_entries(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700417{
418 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
419 int plen = 6;
420 int totalcores = dev_count_cpu();
421 int cores_per_package = get_cores_per_package();
422 int numcpus = totalcores / cores_per_package;
423
424 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
425 numcpus, cores_per_package);
426
427 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
428 for (core_id = 0; core_id < cores_per_package; core_id++) {
429 if (core_id > 0) {
430 pcontrol_blk = 0;
431 plen = 0;
432 }
433
434 /* Generate processor \_PR.CPUx */
435 acpigen_write_processor((cpu_id) * cores_per_package +
436 core_id, pcontrol_blk, plen);
437
438 /* Generate C-state tables */
439 generate_c_state_entries();
440
441 /* Soc specific power states generation */
442 soc_power_states_generation(core_id, cores_per_package);
443
444 acpigen_pop_len();
445 }
446 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100447 /* PPKG is usually used for thermal management
448 of the first and only package. */
449 acpigen_write_processor_package("PPKG", 0, cores_per_package);
450
451 /* Add a method to notify processor nodes */
452 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700453}
454
455#if IS_ENABLED(CONFIG_SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
456/* Save wake source data for ACPI _SWS methods in NVS */
457static void acpi_save_wake_source(void *unused)
458{
459 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
460 uint32_t pm1, *gpe0;
461 int gpe_reg, gpe_reg_count;
462 int reg_size = sizeof(uint32_t) * 8;
463
464 if (!gnvs)
465 return;
466
467 gnvs->pm1i = -1;
468 gnvs->gpei = -1;
469
470 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
471 if (gpe_reg_count < 0)
472 return;
473
474 /* Scan for first set bit in PM1 */
475 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
476 if (pm1 & 1)
477 break;
478 pm1 >>= 1;
479 }
480
481 /* If unable to determine then return -1 */
482 if (gnvs->pm1i >= 16)
483 gnvs->pm1i = -1;
484
485 /* Scan for first set bit in GPE registers */
486 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
487 uint32_t gpe = gpe0[gpe_reg];
488 int start = gpe_reg * reg_size;
489 int end = start + reg_size;
490
491 if (gpe == 0) {
492 if (!gnvs->gpei)
493 gnvs->gpei = end;
494 continue;
495 }
496
497 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
498 if (gpe & 1)
499 break;
500 gpe >>= 1;
501 }
502 }
503
504 /* If unable to determine then return -1 */
505 if (gnvs->gpei >= gpe_reg_count * reg_size)
506 gnvs->gpei = -1;
507
508 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
509 (long long)gnvs->pm1i, (long long)gnvs->gpei);
510}
511
512BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
513
514#endif