blob: 29676b4abfae66eed7897dfc7150c5e8db578286 [file] [log] [blame]
Simon Glassd73344b2020-09-22 12:45:14 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Generic Intel ACPI table generation
4 *
5 * Copyright (C) 2017 Intel Corp.
6 * Copyright 2019 Google LLC
7 *
8 * Modified from coreboot src/soc/intel/common/block/acpi.c
9 */
10
Simon Glassd73344b2020-09-22 12:45:14 -060011#include <bloblist.h>
12#include <cpu.h>
13#include <dm.h>
14#include <acpi/acpigen.h>
15#include <asm/acpigen.h>
16#include <asm/acpi_table.h>
17#include <asm/cpu.h>
18#include <asm/cpu_common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glassd73344b2020-09-22 12:45:14 -060020#include <asm/intel_acpi.h>
21#include <asm/ioapic.h>
22#include <asm/mpspec.h>
23#include <asm/smm.h>
24#include <asm/turbo.h>
25#include <asm/intel_gnvs.h>
26#include <asm/arch/iomap.h>
27#include <asm/arch/pm.h>
28#include <asm/arch/systemagent.h>
29#include <dm/acpi.h>
30#include <linux/err.h>
31#include <power/acpi_pmc.h>
32
Moritz Fischerc6561722022-02-05 12:17:45 -080033int acpi_fill_mcfg(struct acpi_ctx *ctx)
Simon Glassd73344b2020-09-22 12:45:14 -060034{
Moritz Fischerc6561722022-02-05 12:17:45 -080035 size_t size;
36
Simon Glassd73344b2020-09-22 12:45:14 -060037 /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */
Moritz Fischerc6561722022-02-05 12:17:45 -080038 size = acpi_create_mcfg_mmconfig((void *)ctx->current,
39 CONFIG_MMCONF_BASE_ADDRESS, 0, 0,
40 (CONFIG_SA_PCIEX_LENGTH >> 20) - 1);
41 acpi_inc(ctx, size);
42
43 return 0;
Simon Glassd73344b2020-09-22 12:45:14 -060044}
45
46static int acpi_sci_irq(void)
47{
48 int sci_irq = 9;
49 uint scis;
50 int ret;
51
52 ret = arch_read_sci_irq_select();
53 if (IS_ERR_VALUE(ret))
54 return log_msg_ret("sci_irq", ret);
55 scis = ret;
56 scis &= SCI_IRQ_MASK;
57 scis >>= SCI_IRQ_SHIFT;
58
59 /* Determine how SCI is routed. */
60 switch (scis) {
61 case SCIS_IRQ9:
62 case SCIS_IRQ10:
63 case SCIS_IRQ11:
64 sci_irq = scis - SCIS_IRQ9 + 9;
65 break;
66 case SCIS_IRQ20:
67 case SCIS_IRQ21:
68 case SCIS_IRQ22:
69 case SCIS_IRQ23:
70 sci_irq = scis - SCIS_IRQ20 + 20;
71 break;
72 default:
73 log_warning("Invalid SCI route! Defaulting to IRQ9\n");
74 sci_irq = 9;
75 break;
76 }
77
78 log_debug("SCI is IRQ%d\n", sci_irq);
79
80 return sci_irq;
81}
82
83static unsigned long acpi_madt_irq_overrides(unsigned long current)
84{
85 int sci = acpi_sci_irq();
86 u16 flags = MP_IRQ_TRIGGER_LEVEL;
87
88 if (sci < 0)
89 return log_msg_ret("sci irq", sci);
90
91 /* INT_SRC_OVR */
92 current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0);
93
94 flags |= arch_madt_sci_irq_polarity(sci);
95
96 /* SCI */
97 current +=
98 acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags);
99
100 return current;
101}
102
103u32 acpi_fill_madt(u32 current)
104{
105 /* Local APICs */
106 current += acpi_create_madt_lapics(current);
107
108 /* IOAPIC */
109 current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0);
110
111 return acpi_madt_irq_overrides(current);
112}
113
114void intel_acpi_fill_fadt(struct acpi_fadt *fadt)
115{
116 const u16 pmbase = IOMAP_ACPI_BASE;
117
118 /* Use ACPI 3.0 revision. */
119 fadt->header.revision = acpi_get_table_revision(ACPITAB_FADT);
120
121 fadt->sci_int = acpi_sci_irq();
122 fadt->smi_cmd = APM_CNT;
123 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
124 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
125 fadt->s4bios_req = 0x0;
126 fadt->pstate_cnt = 0;
127
128 fadt->pm1a_evt_blk = pmbase + PM1_STS;
129 fadt->pm1b_evt_blk = 0x0;
130 fadt->pm1a_cnt_blk = pmbase + PM1_CNT;
131 fadt->pm1b_cnt_blk = 0x0;
132
133 fadt->gpe0_blk = pmbase + GPE0_STS;
134
135 fadt->pm1_evt_len = 4;
136 fadt->pm1_cnt_len = 2;
137
138 /* GPE0 STS/EN pairs each 32 bits wide. */
139 fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t);
140
141 fadt->flush_size = 0x400; /* twice of cache size */
142 fadt->flush_stride = 0x10; /* Cache line width */
143 fadt->duty_offset = 1;
144 fadt->day_alrm = 0xd;
145
146 fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
147 ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON |
148 ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE |
149 ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK;
150
151 fadt->reset_reg.space_id = 1;
152 fadt->reset_reg.bit_width = 8;
153 fadt->reset_reg.addrl = IO_PORT_RESET;
154 fadt->reset_value = RST_CPU | SYS_RST;
155
156 fadt->x_pm1a_evt_blk.space_id = 1;
157 fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8;
158 fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS;
159
160 fadt->x_pm1b_evt_blk.space_id = 1;
161
162 fadt->x_pm1a_cnt_blk.space_id = 1;
163 fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8;
164 fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT;
165
166 fadt->x_pm1b_cnt_blk.space_id = 1;
167
168 fadt->x_gpe1_blk.space_id = 1;
169}
170
171int intel_southbridge_write_acpi_tables(const struct udevice *dev,
172 struct acpi_ctx *ctx)
173{
174 int ret;
175
176 ret = acpi_write_dbg2_pci_uart(ctx, gd->cur_serial_dev,
177 ACPI_ACCESS_SIZE_DWORD_ACCESS);
178 if (ret)
179 return log_msg_ret("dbg2", ret);
180
181 ret = acpi_write_hpet(ctx);
182 if (ret)
183 return log_msg_ret("hpet", ret);
184
185 return 0;
186}
187
188__weak u32 acpi_fill_soc_wake(u32 generic_pm1_en,
189 const struct chipset_power_state *ps)
190{
191 return generic_pm1_en;
192}
193
194__weak int acpi_create_gnvs(struct acpi_global_nvs *gnvs)
195{
196 return 0;
197}
198
199int southbridge_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
200{
201 struct acpi_global_nvs *gnvs;
202 int ret;
203
Simon Glass06373972020-09-19 18:49:29 -0600204 ret = bloblist_ensure_size(BLOBLISTT_ACPI_GNVS, sizeof(*gnvs), 0,
Simon Glassd73344b2020-09-22 12:45:14 -0600205 (void **)&gnvs);
206 if (ret)
207 return log_msg_ret("bloblist", ret);
Simon Glassd73344b2020-09-22 12:45:14 -0600208
209 ret = acpi_create_gnvs(gnvs);
210 if (ret)
211 return log_msg_ret("gnvs", ret);
212
213 /*
214 * TODO(sjg@chromum.org): tell SMI about it
215 * smm_setup_structures(gnvs, NULL, NULL);
216 */
217
218 /* Add it to DSDT */
219 acpigen_write_scope(ctx, "\\");
220 acpigen_write_name_dword(ctx, "NVSA", (uintptr_t)gnvs);
221 acpigen_pop_len(ctx);
222
223 return 0;
224}
225
226static int calculate_power(int tdp, int p1_ratio, int ratio)
227{
228 u32 m;
229 u32 power;
230
231 /*
232 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
233 *
234 * Power = (ratio / p1_ratio) * m * tdp
235 */
236
237 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
238 m = (m * m) / 1000;
239
240 power = ((ratio * 100000 / p1_ratio) / 100);
241 power *= (m / 100) * (tdp / 1000);
242 power /= 1000;
243
244 return power;
245}
246
247void generate_p_state_entries(struct acpi_ctx *ctx, int core,
248 int cores_per_package)
249{
250 int ratio_min, ratio_max, ratio_turbo, ratio_step;
251 int coord_type, power_max, num_entries;
252 int ratio, power, clock, clock_max;
253 bool turbo;
254
255 coord_type = cpu_get_coord_type();
256 ratio_min = cpu_get_min_ratio();
257 ratio_max = cpu_get_max_ratio();
258 clock_max = (ratio_max * cpu_get_bus_clock_khz()) / 1000;
259 turbo = (turbo_get_state() == TURBO_ENABLED);
260
261 /* Calculate CPU TDP in mW */
262 power_max = cpu_get_power_max();
263
264 /* Write _PCT indicating use of FFixedHW */
265 acpigen_write_empty_pct(ctx);
266
267 /* Write _PPC with no limit on supported P-state */
268 acpigen_write_ppc_nvs(ctx);
269 /* Write PSD indicating configured coordination type */
270 acpigen_write_psd_package(ctx, core, 1, coord_type);
271
272 /* Add P-state entries in _PSS table */
273 acpigen_write_name(ctx, "_PSS");
274
275 /* Determine ratio points */
276 ratio_step = PSS_RATIO_STEP;
277 do {
278 num_entries = ((ratio_max - ratio_min) / ratio_step) + 1;
279 if (((ratio_max - ratio_min) % ratio_step) > 0)
280 num_entries += 1;
281 if (turbo)
282 num_entries += 1;
283 if (num_entries > PSS_MAX_ENTRIES)
284 ratio_step += 1;
285 } while (num_entries > PSS_MAX_ENTRIES);
286
287 /* _PSS package count depends on Turbo */
288 acpigen_write_package(ctx, num_entries);
289
290 /* P[T] is Turbo state if enabled */
291 if (turbo) {
292 ratio_turbo = cpu_get_max_turbo_ratio();
293
294 /* Add entry for Turbo ratio */
295 acpigen_write_pss_package(ctx, clock_max + 1, /* MHz */
296 power_max, /* mW */
297 PSS_LATENCY_TRANSITION,/* lat1 */
298 PSS_LATENCY_BUSMASTER,/* lat2 */
299 ratio_turbo << 8, /* control */
300 ratio_turbo << 8); /* status */
301 num_entries -= 1;
302 }
303
304 /* First regular entry is max non-turbo ratio */
305 acpigen_write_pss_package(ctx, clock_max, /* MHz */
306 power_max, /* mW */
307 PSS_LATENCY_TRANSITION,/* lat1 */
308 PSS_LATENCY_BUSMASTER,/* lat2 */
309 ratio_max << 8, /* control */
310 ratio_max << 8); /* status */
311 num_entries -= 1;
312
313 /* Generate the remaining entries */
314 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
315 ratio >= ratio_min; ratio -= ratio_step) {
316 /* Calculate power at this ratio */
317 power = calculate_power(power_max, ratio_max, ratio);
318 clock = (ratio * cpu_get_bus_clock_khz()) / 1000;
319
320 acpigen_write_pss_package(ctx, clock, /* MHz */
321 power, /* mW */
322 PSS_LATENCY_TRANSITION,/* lat1 */
323 PSS_LATENCY_BUSMASTER,/* lat2 */
324 ratio << 8, /* control */
325 ratio << 8); /* status */
326 }
327 /* Fix package length */
328 acpigen_pop_len(ctx);
329}
330
331void generate_t_state_entries(struct acpi_ctx *ctx, int core,
332 int cores_per_package, struct acpi_tstate *entry,
333 int nentries)
334{
335 if (!nentries)
336 return;
337
338 /* Indicate SW_ALL coordination for T-states */
339 acpigen_write_tsd_package(ctx, core, cores_per_package, SW_ALL);
340
341 /* Indicate FixedHW so OS will use MSR */
342 acpigen_write_empty_ptc(ctx);
343
344 /* Set NVS controlled T-state limit */
345 acpigen_write_tpc(ctx, "\\TLVL");
346
347 /* Write TSS table for MSR access */
348 acpigen_write_tss_package(ctx, entry, nentries);
349}
350
351int acpi_generate_cpu_header(struct acpi_ctx *ctx, int core_id,
352 const struct acpi_cstate *c_state_map,
353 int num_cstates)
354{
355 bool is_first = !core_id;
356
357 /* Generate processor \_PR.CPUx */
358 acpigen_write_processor(ctx, core_id, is_first ? ACPI_BASE_ADDRESS : 0,
359 is_first ? 6 : 0);
360
361 /* Generate C-state tables */
362 acpigen_write_cst_package(ctx, c_state_map, num_cstates);
363
364 return 0;
365}
366
367int acpi_generate_cpu_package_final(struct acpi_ctx *ctx, int cores_per_package)
368{
369 /*
370 * PPKG is usually used for thermal management of the first and only
371 * package
372 */
373 acpigen_write_processor_package(ctx, "PPKG", 0, cores_per_package);
374
375 /* Add a method to notify processor nodes */
376 acpigen_write_processor_cnot(ctx, cores_per_package);
377
378 return 0;
379}