blob: 0ea6cb9d701ae8bdd412bb25c583c409c0380f13 [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
34unsigned long acpi_fill_mcfg(unsigned long current)
35{
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
183/*
184 * Save wake source information for calculating ACPI _SWS values
185 *
186 * @pm1: PM1_STS register with only enabled events set
187 * @gpe0: GPE0_STS registers with only enabled events set
188 *
189 * return the number of registers in the gpe0 array or -1 if nothing
190 * is provided by this function.
191 */
192
193static int acpi_fill_wake(uint32_t *pm1, uint32_t **gpe0)
194{
195 struct chipset_power_state *ps;
196 static uint32_t gpe0_sts[GPE0_REG_MAX];
197 uint32_t pm1_en;
198 int i;
199
200 ps = cbmem_find(CBMEM_ID_POWER_STATE);
201 if (ps == NULL)
202 return -1;
203
204 /*
205 * PM1_EN to check the basic wake events which can happen through
206 * powerbtn or any other wake source like lidopen, key board press etc.
207 */
208 pm1_en = ps->pm1_en;
209
210 pm1_en = acpi_fill_soc_wake(pm1_en, ps);
211
212 *pm1 = ps->pm1_sts & pm1_en;
213
214 /* Mask off GPE0 status bits that are not enabled */
215 *gpe0 = &gpe0_sts[0];
216 for (i = 0; i < GPE0_REG_MAX; i++)
217 gpe0_sts[i] = ps->gpe0_sts[i] & ps->gpe0_en[i];
218
219 return GPE0_REG_MAX;
220}
221
Aaron Durbin64031672018-04-21 14:45:32 -0600222__weak void acpi_create_gnvs(struct global_nvs_t *gnvs)
Shaunak Sahabd427802017-07-18 00:19:33 -0700223{
224}
225
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200226void southbridge_inject_dsdt(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700227{
228 struct global_nvs_t *gnvs;
229
230 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
231 if (!gnvs) {
232 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
233 if (gnvs)
234 memset(gnvs, 0, sizeof(*gnvs));
235 }
236
237 if (gnvs) {
238 acpi_create_gnvs(gnvs);
Shaunak Sahabd427802017-07-18 00:19:33 -0700239 /* And tell SMI about it */
240 smm_setup_structures(gnvs, NULL, NULL);
241
242 /* Add it to DSDT. */
243 acpigen_write_scope("\\");
244 acpigen_write_name_dword("NVSA", (uintptr_t) gnvs);
245 acpigen_pop_len();
246 }
247}
248
249static int calculate_power(int tdp, int p1_ratio, int ratio)
250{
251 u32 m;
252 u32 power;
253
254 /*
255 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
256 *
257 * Power = (ratio / p1_ratio) * m * tdp
258 */
259
260 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
261 m = (m * m) / 1000;
262
263 power = ((ratio * 100000 / p1_ratio) / 100);
264 power *= (m / 100) * (tdp / 1000);
265 power /= 1000;
266
267 return power;
268}
269
270static int get_cores_per_package(void)
271{
272 struct cpuinfo_x86 c;
273 struct cpuid_result result;
274 int cores = 1;
275
276 get_fms(&c, cpuid_eax(1));
277 if (c.x86 != 6)
278 return 1;
279
280 result = cpuid_ext(0xb, 1);
281 cores = result.ebx & 0xff;
282
283 return cores;
284}
285
286static void generate_c_state_entries(void)
287{
288 acpi_cstate_t *c_state_map;
289 size_t entries;
290
291 c_state_map = soc_get_cstate_map(&entries);
292
293 /* Generate C-state tables */
294 acpigen_write_CST_package(c_state_map, entries);
295}
296
297void generate_p_state_entries(int core, int cores_per_package)
298{
299 int ratio_min, ratio_max, ratio_turbo, ratio_step;
300 int coord_type, power_max, num_entries;
301 int ratio, power, clock, clock_max;
302
303 coord_type = cpu_get_coord_type();
304 ratio_min = cpu_get_min_ratio();
305 ratio_max = cpu_get_max_ratio();
306 clock_max = (ratio_max * cpu_get_bus_clock()) / KHz;
307
308 /* Calculate CPU TDP in mW */
309 power_max = cpu_get_power_max();
310
311 /* Write _PCT indicating use of FFixedHW */
312 acpigen_write_empty_PCT();
313
314 /* Write _PPC with no limit on supported P-state */
315 acpigen_write_PPC_NVS();
316 /* Write PSD indicating configured coordination type */
317 acpigen_write_PSD_package(core, 1, coord_type);
318
319 /* Add P-state entries in _PSS table */
320 acpigen_write_name("_PSS");
321
322 /* Determine ratio points */
323 ratio_step = PSS_RATIO_STEP;
324 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
325 if (num_entries > PSS_MAX_ENTRIES) {
326 ratio_step += 1;
327 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
328 }
329
330 /* P[T] is Turbo state if enabled */
331 if (get_turbo_state() == TURBO_ENABLED) {
332 /* _PSS package count including Turbo */
333 acpigen_write_package(num_entries + 2);
334 ratio_turbo = cpu_get_max_turbo_ratio();
335
336 /* Add entry for Turbo ratio */
337 acpigen_write_PSS_package(clock_max + 1, /* MHz */
338 power_max, /* mW */
339 PSS_LATENCY_TRANSITION,/* lat1 */
340 PSS_LATENCY_BUSMASTER,/* lat2 */
341 ratio_turbo << 8, /* control */
342 ratio_turbo << 8); /* status */
343 } else {
344 /* _PSS package count without Turbo */
345 acpigen_write_package(num_entries + 1);
346 }
347
348 /* First regular entry is max non-turbo ratio */
349 acpigen_write_PSS_package(clock_max, /* MHz */
350 power_max, /* mW */
351 PSS_LATENCY_TRANSITION,/* lat1 */
352 PSS_LATENCY_BUSMASTER,/* lat2 */
353 ratio_max << 8, /* control */
354 ratio_max << 8); /* status */
355
356 /* Generate the remaining entries */
357 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
358 ratio >= ratio_min; ratio -= ratio_step) {
359
360 /* Calculate power at this ratio */
361 power = calculate_power(power_max, ratio_max, ratio);
362 clock = (ratio * cpu_get_bus_clock()) / KHz;
363
364 acpigen_write_PSS_package(clock, /* MHz */
365 power, /* mW */
366 PSS_LATENCY_TRANSITION,/* lat1 */
367 PSS_LATENCY_BUSMASTER,/* lat2 */
368 ratio << 8, /* control */
369 ratio << 8); /* status */
370 }
371 /* Fix package length */
372 acpigen_pop_len();
373}
374
375static acpi_tstate_t *soc_get_tss_table(int *entries)
376{
377 *entries = 0;
378 return NULL;
379}
380
381void generate_t_state_entries(int core, int cores_per_package)
382{
383 acpi_tstate_t *soc_tss_table;
384 int entries;
385
386 soc_tss_table = soc_get_tss_table(&entries);
387 if (entries == 0)
388 return;
389
390 /* Indicate SW_ALL coordination for T-states */
391 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
392
393 /* Indicate FixedHW so OS will use MSR */
394 acpigen_write_empty_PTC();
395
396 /* Set NVS controlled T-state limit */
397 acpigen_write_TPC("\\TLVL");
398
399 /* Write TSS table for MSR access */
400 acpigen_write_TSS_package(entries, soc_tss_table);
401}
402
Aaron Durbin64031672018-04-21 14:45:32 -0600403__weak void soc_power_states_generation(int core_id,
Shaunak Sahabd427802017-07-18 00:19:33 -0700404 int cores_per_package)
405{
406}
407
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200408void generate_cpu_entries(struct device *device)
Shaunak Sahabd427802017-07-18 00:19:33 -0700409{
410 int core_id, cpu_id, pcontrol_blk = ACPI_BASE_ADDRESS;
411 int plen = 6;
412 int totalcores = dev_count_cpu();
413 int cores_per_package = get_cores_per_package();
414 int numcpus = totalcores / cores_per_package;
415
416 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
417 numcpus, cores_per_package);
418
419 for (cpu_id = 0; cpu_id < numcpus; cpu_id++) {
420 for (core_id = 0; core_id < cores_per_package; core_id++) {
421 if (core_id > 0) {
422 pcontrol_blk = 0;
423 plen = 0;
424 }
425
426 /* Generate processor \_PR.CPUx */
427 acpigen_write_processor((cpu_id) * cores_per_package +
428 core_id, pcontrol_blk, plen);
429
430 /* Generate C-state tables */
431 generate_c_state_entries();
432
433 /* Soc specific power states generation */
434 soc_power_states_generation(core_id, cores_per_package);
435
436 acpigen_pop_len();
437 }
438 }
Arthur Heymans0ac555e2018-11-28 12:25:54 +0100439 /* PPKG is usually used for thermal management
440 of the first and only package. */
441 acpigen_write_processor_package("PPKG", 0, cores_per_package);
442
443 /* Add a method to notify processor nodes */
444 acpigen_write_processor_cnot(cores_per_package);
Shaunak Sahabd427802017-07-18 00:19:33 -0700445}
446
447#if IS_ENABLED(CONFIG_SOC_INTEL_COMMON_ACPI_WAKE_SOURCE)
448/* Save wake source data for ACPI _SWS methods in NVS */
449static void acpi_save_wake_source(void *unused)
450{
451 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
452 uint32_t pm1, *gpe0;
453 int gpe_reg, gpe_reg_count;
454 int reg_size = sizeof(uint32_t) * 8;
455
456 if (!gnvs)
457 return;
458
459 gnvs->pm1i = -1;
460 gnvs->gpei = -1;
461
462 gpe_reg_count = acpi_fill_wake(&pm1, &gpe0);
463 if (gpe_reg_count < 0)
464 return;
465
466 /* Scan for first set bit in PM1 */
467 for (gnvs->pm1i = 0; gnvs->pm1i < reg_size; gnvs->pm1i++) {
468 if (pm1 & 1)
469 break;
470 pm1 >>= 1;
471 }
472
473 /* If unable to determine then return -1 */
474 if (gnvs->pm1i >= 16)
475 gnvs->pm1i = -1;
476
477 /* Scan for first set bit in GPE registers */
478 for (gpe_reg = 0; gpe_reg < gpe_reg_count; gpe_reg++) {
479 uint32_t gpe = gpe0[gpe_reg];
480 int start = gpe_reg * reg_size;
481 int end = start + reg_size;
482
483 if (gpe == 0) {
484 if (!gnvs->gpei)
485 gnvs->gpei = end;
486 continue;
487 }
488
489 for (gnvs->gpei = start; gnvs->gpei < end; gnvs->gpei++) {
490 if (gpe & 1)
491 break;
492 gpe >>= 1;
493 }
494 }
495
496 /* If unable to determine then return -1 */
497 if (gnvs->gpei >= gpe_reg_count * reg_size)
498 gnvs->gpei = -1;
499
500 printk(BIOS_DEBUG, "ACPI _SWS is PM1 Index %lld GPE Index %lld\n",
501 (long long)gnvs->pm1i, (long long)gnvs->gpei);
502}
503
504BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, acpi_save_wake_source, NULL);
505
506#endif