blob: ca5d7268743c8561737840dd16ab9bbf1ac3fa81 [file] [log] [blame]
Subrata Banik2871e0e2020-09-27 11:30:58 +05301/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <acpi/acpi.h>
4#include <acpi/acpi_gnvs.h>
5#include <acpi/acpigen.h>
6#include <device/mmio.h>
7#include <arch/smp/mpspec.h>
8#include <cbmem.h>
9#include <console/console.h>
10#include <device/device.h>
11#include <device/pci_ops.h>
12#include <ec/google/chromeec/ec.h>
13#include <intelblocks/cpulib.h>
14#include <intelblocks/pmclib.h>
15#include <intelblocks/acpi.h>
16#include <soc/cpu.h>
17#include <soc/iomap.h>
18#include <soc/nvs.h>
19#include <soc/pci_devs.h>
20#include <soc/pm.h>
21#include <soc/soc_chip.h>
22#include <soc/systemagent.h>
23#include <string.h>
24#include <types.h>
25#include <wrdd.h>
26
27/*
28 * List of supported C-states in this processor.
29 */
30enum {
31 C_STATE_C0, /* 0 */
32 C_STATE_C1, /* 1 */
33 C_STATE_C1E, /* 2 */
34 C_STATE_C6_SHORT_LAT, /* 3 */
35 C_STATE_C6_LONG_LAT, /* 4 */
36 C_STATE_C7_SHORT_LAT, /* 5 */
37 C_STATE_C7_LONG_LAT, /* 6 */
38 C_STATE_C7S_SHORT_LAT, /* 7 */
39 C_STATE_C7S_LONG_LAT, /* 8 */
40 C_STATE_C8, /* 9 */
41 C_STATE_C9, /* 10 */
42 C_STATE_C10, /* 11 */
43 NUM_C_STATES
44};
45
46#define MWAIT_RES(state, sub_state) \
47 { \
48 .addrl = (((state) << 4) | (sub_state)), \
49 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
50 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
51 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
52 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
53 }
54
55static const acpi_cstate_t cstate_map[NUM_C_STATES] = {
56 [C_STATE_C0] = {},
57 [C_STATE_C1] = {
58 .latency = C1_LATENCY,
59 .power = C1_POWER,
60 .resource = MWAIT_RES(0, 0),
61 },
62 [C_STATE_C1E] = {
63 .latency = C1_LATENCY,
64 .power = C1_POWER,
65 .resource = MWAIT_RES(0, 1),
66 },
67 [C_STATE_C6_SHORT_LAT] = {
68 .latency = C6_LATENCY,
69 .power = C6_POWER,
70 .resource = MWAIT_RES(2, 0),
71 },
72 [C_STATE_C6_LONG_LAT] = {
73 .latency = C6_LATENCY,
74 .power = C6_POWER,
75 .resource = MWAIT_RES(2, 1),
76 },
77 [C_STATE_C7_SHORT_LAT] = {
78 .latency = C7_LATENCY,
79 .power = C7_POWER,
80 .resource = MWAIT_RES(3, 0),
81 },
82 [C_STATE_C7_LONG_LAT] = {
83 .latency = C7_LATENCY,
84 .power = C7_POWER,
85 .resource = MWAIT_RES(3, 1),
86 },
87 [C_STATE_C7S_SHORT_LAT] = {
88 .latency = C7_LATENCY,
89 .power = C7_POWER,
90 .resource = MWAIT_RES(3, 2),
91 },
92 [C_STATE_C7S_LONG_LAT] = {
93 .latency = C7_LATENCY,
94 .power = C7_POWER,
95 .resource = MWAIT_RES(3, 3),
96 },
97 [C_STATE_C8] = {
98 .latency = C8_LATENCY,
99 .power = C8_POWER,
100 .resource = MWAIT_RES(4, 0),
101 },
102 [C_STATE_C9] = {
103 .latency = C9_LATENCY,
104 .power = C9_POWER,
105 .resource = MWAIT_RES(5, 0),
106 },
107 [C_STATE_C10] = {
108 .latency = C10_LATENCY,
109 .power = C10_POWER,
110 .resource = MWAIT_RES(6, 0),
111 },
112};
113
114static int cstate_set_non_s0ix[] = {
115 C_STATE_C1,
116 C_STATE_C6_LONG_LAT,
117 C_STATE_C7S_LONG_LAT
118};
119
120static int cstate_set_s0ix[] = {
121 C_STATE_C1,
122 C_STATE_C7S_LONG_LAT,
123 C_STATE_C10
124};
125
126acpi_cstate_t *soc_get_cstate_map(size_t *entries)
127{
128 static acpi_cstate_t map[MAX(ARRAY_SIZE(cstate_set_s0ix),
129 ARRAY_SIZE(cstate_set_non_s0ix))];
130 int *set;
131 int i;
132
133 config_t *config = config_of_soc();
134
135 int is_s0ix_enable = config->s0ix_enable;
136
137 if (is_s0ix_enable) {
138 *entries = ARRAY_SIZE(cstate_set_s0ix);
139 set = cstate_set_s0ix;
140 } else {
141 *entries = ARRAY_SIZE(cstate_set_non_s0ix);
142 set = cstate_set_non_s0ix;
143 }
144
145 for (i = 0; i < *entries; i++) {
146 memcpy(&map[i], &cstate_map[set[i]], sizeof(acpi_cstate_t));
147 map[i].ctype = i + 1;
148 }
149 return map;
150}
151
152void soc_power_states_generation(int core_id, int cores_per_package)
153{
154 config_t *config = config_of_soc();
155
156 if (config->eist_enable)
157 /* Generate P-state tables */
158 generate_p_state_entries(core_id, cores_per_package);
159}
160
161void soc_fill_fadt(acpi_fadt_t *fadt)
162{
163 const uint16_t pmbase = ACPI_BASE_ADDRESS;
164
165 config_t *config = config_of_soc();
166
167 fadt->pm_tmr_blk = pmbase + PM1_TMR;
168 fadt->pm_tmr_len = 4;
169 fadt->x_pm_tmr_blk.space_id = ACPI_ADDRESS_SPACE_IO;
170 fadt->x_pm_tmr_blk.bit_width = fadt->pm_tmr_len * 8;
171 fadt->x_pm_tmr_blk.bit_offset = 0;
172 fadt->x_pm_tmr_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
173 fadt->x_pm_tmr_blk.addrl = pmbase + PM1_TMR;
174 fadt->x_pm_tmr_blk.addrh = 0x0;
175
176 if (config->s0ix_enable)
177 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
178}
179
180uint32_t soc_read_sci_irq_select(void)
181{
182 return read32((void *)soc_read_pmc_base() + IRQ_REG);
183}
184
185static unsigned long soc_fill_dmar(unsigned long current)
186{
187 const struct device *const igfx_dev = pcidev_path_on_root(SA_DEVFN_IGD);
188 const uint64_t gfxvtbar = MCHBAR64(GFXVTBAR) & VTBAR_MASK;
189 const bool gfxvten = MCHBAR32(GFXVTBAR) & VTBAR_ENABLED;
190
191 if (is_dev_enabled(igfx_dev) && gfxvtbar && gfxvten) {
192 const unsigned long tmp = current;
193
194 current += acpi_create_dmar_drhd(current, 0, 0, gfxvtbar);
195 current += acpi_create_dmar_ds_pci(current, 0, 2, 0);
196
197 acpi_dmar_drhd_fixup(tmp, current);
198 }
199
200 const struct device *const ipu_dev = pcidev_path_on_root(SA_DEVFN_IPU);
201 const uint64_t ipuvtbar = MCHBAR64(IPUVTBAR) & VTBAR_MASK;
202 const bool ipuvten = MCHBAR32(IPUVTBAR) & VTBAR_ENABLED;
203
204 if (is_dev_enabled(ipu_dev) && ipuvtbar && ipuvten) {
205 const unsigned long tmp = current;
206
207 current += acpi_create_dmar_drhd(current, 0, 0, ipuvtbar);
208 current += acpi_create_dmar_ds_pci(current, 0, 5, 0);
209
210 acpi_dmar_drhd_fixup(tmp, current);
211 }
212
213 const uint64_t vtvc0bar = MCHBAR64(VTVC0BAR) & VTBAR_MASK;
214 const bool vtvc0en = MCHBAR32(VTVC0BAR) & VTBAR_ENABLED;
215
216 if (vtvc0bar && vtvc0en) {
217 const unsigned long tmp = current;
218
219 current += acpi_create_dmar_drhd(current,
220 DRHD_INCLUDE_PCI_ALL, 0, vtvc0bar);
221 current += acpi_create_dmar_ds_ioapic(current,
222 2, V_P2SB_CFG_IBDF_BUS, V_P2SB_CFG_IBDF_DEV,
223 V_P2SB_CFG_IBDF_FUNC);
224 current += acpi_create_dmar_ds_msi_hpet(current,
225 0, V_P2SB_CFG_HBDF_BUS, V_P2SB_CFG_HBDF_DEV,
226 V_P2SB_CFG_HBDF_FUNC);
227
228 acpi_dmar_drhd_fixup(tmp, current);
229 }
230
231 /* TCSS Thunderbolt root ports */
232 for (unsigned int i = 0; i < MAX_TBT_PCIE_PORT; i++) {
233 const struct device *const tbt_dev = pcidev_path_on_root(SA_DEVFN_TBT(i));
234 if (is_dev_enabled(tbt_dev)) {
235 const uint64_t tbtbar = MCHBAR64(TBTxBAR(i)) & VTBAR_MASK;
236 const bool tbten = MCHBAR32(TBTxBAR(i)) & VTBAR_ENABLED;
237 if (tbtbar && tbten) {
238 const unsigned long tmp = current;
239
240 current += acpi_create_dmar_drhd(current, 0, 0, tbtbar);
241 current += acpi_create_dmar_ds_pci_br(current, 0, 7, i);
242
243 acpi_dmar_drhd_fixup(tmp, current);
244 }
245 }
246 }
247
248 /* Add RMRR entry */
249 if (is_dev_enabled(igfx_dev)) {
250 const unsigned long tmp = current;
251 current += acpi_create_dmar_rmrr(current, 0,
252 sa_get_gsm_base(), sa_get_tolud_base() - 1);
253 current += acpi_create_dmar_ds_pci(current, 0, 2, 0);
254 acpi_dmar_rmrr_fixup(tmp, current);
255 }
256
257 return current;
258}
259
260unsigned long sa_write_acpi_tables(const struct device *dev, unsigned long current,
261 struct acpi_rsdp *rsdp)
262{
263 acpi_dmar_t *const dmar = (acpi_dmar_t *)current;
264
265 /*
266 * Create DMAR table only if we have VT-d capability and FSP does not override its
267 * feature.
268 */
269 if ((pci_read_config32(dev, CAPID0_A) & VTD_DISABLE) ||
270 !(MCHBAR32(VTVC0BAR) & VTBAR_ENABLED))
271 return current;
272
273 printk(BIOS_DEBUG, "ACPI: * DMAR\n");
274 acpi_create_dmar(dmar, DMAR_INTR_REMAP | DMA_CTRL_PLATFORM_OPT_IN_FLAG, soc_fill_dmar);
275 current += dmar->header.length;
276 current = acpi_align_current(current);
277 acpi_add_table(rsdp, dmar);
278
279 return current;
280}
281
282void acpi_create_gnvs(struct global_nvs *gnvs)
283{
284 config_t *config = config_of_soc();
285
286 /* Set unknown wake source */
287 gnvs->pm1i = -1;
288
289 /* CPU core count */
290 gnvs->pcnt = dev_count_cpu();
291
292 if (CONFIG(CONSOLE_CBMEM))
293 /* Update the mem console pointer. */
294 gnvs->cbmc = (uintptr_t)cbmem_find(CBMEM_ID_CONSOLE);
295
296 if (CONFIG(CHROMEOS)) {
297 /* Initialize Verified Boot data */
298 chromeos_init_chromeos_acpi(&(gnvs->chromeos));
299 if (CONFIG(EC_GOOGLE_CHROMEEC)) {
300 gnvs->chromeos.vbt2 = google_ec_running_ro() ?
301 ACTIVE_ECFW_RO : ACTIVE_ECFW_RW;
302 } else
303 gnvs->chromeos.vbt2 = ACTIVE_ECFW_RO;
304 }
305
306 /* Enable DPTF based on mainboard configuration */
307 gnvs->dpte = config->dptf_enable;
308
309 /* Fill in the Wifi Region id */
310 gnvs->cid1 = wifi_regulatory_domain();
311
312 /* Set USB2/USB3 wake enable bitmaps. */
313 gnvs->u2we = config->usb2_wake_enable_bitmap;
314 gnvs->u3we = config->usb3_wake_enable_bitmap;
315
316 /* Fill in Above 4GB MMIO resource */
317 sa_fill_gnvs(gnvs);
318}
319
320uint32_t acpi_fill_soc_wake(uint32_t generic_pm1_en,
321 const struct chipset_power_state *ps)
322{
323 /*
324 * WAK_STS bit is set when the system is in one of the sleep states
325 * (via the SLP_EN bit) and an enabled wake event occurs. Upon setting
326 * this bit, the PMC will transition the system to the ON state and
327 * can only be set by hardware and can only be cleared by writing a one
328 * to this bit position.
329 */
330
331 generic_pm1_en |= WAK_STS | RTC_EN | PWRBTN_EN;
332 return generic_pm1_en;
333}
334
335int soc_madt_sci_irq_polarity(int sci)
336{
337 return MP_IRQ_POLARITY_HIGH;
338}
339
340static int acpigen_soc_gpio_op(const char *op, unsigned int gpio_num)
341{
342 /* op (gpio_num) */
343 acpigen_emit_namestring(op);
344 acpigen_write_integer(gpio_num);
345 return 0;
346}
347
348static int acpigen_soc_get_gpio_state(const char *op, unsigned int gpio_num)
349{
350 /* Store (op (gpio_num), Local0) */
351 acpigen_write_store();
352 acpigen_soc_gpio_op(op, gpio_num);
353 acpigen_emit_byte(LOCAL0_OP);
354 return 0;
355}
356
357int acpigen_soc_read_rx_gpio(unsigned int gpio_num)
358{
359 return acpigen_soc_get_gpio_state("\\_SB.PCI0.GRXS", gpio_num);
360}
361
362int acpigen_soc_get_tx_gpio(unsigned int gpio_num)
363{
364 return acpigen_soc_get_gpio_state("\\_SB.PCI0.GTXS", gpio_num);
365}
366
367int acpigen_soc_set_tx_gpio(unsigned int gpio_num)
368{
369 return acpigen_soc_gpio_op("\\_SB.PCI0.STXS", gpio_num);
370}
371
372int acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
373{
374 return acpigen_soc_gpio_op("\\_SB.PCI0.CTXS", gpio_num);
375}