Debian Codegraph @

src/soc/intel/common/block/pmc/pmclib.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpi_pm.h>
4 #include <arch/io.h>
5 #include <assert.h>
6 #include <bootmode.h>
7 #include <device/mmio.h>
8 #include <device/pci.h>
9 #include <cbmem.h>
10 #include <cpu/x86/smm.h>
11 #include <console/console.h>
12 #include <halt.h>
13 #include <intelblocks/pmc_ipc.h>
14 #include <intelblocks/pmclib.h>
15 #include <intelblocks/gpio.h>
16 #include <intelblocks/tco.h>
17 #include <option.h>
18 #include <reset.h>
19 #include <security/vboot/vboot_common.h>
20 #include <soc/pci_devs.h>
21 #include <soc/pm.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <timer.h>
25
26 #define PMC_IPC_BIOS_RST_COMPLETE 0xd0
27 #define PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE 0
28 #define PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM BIT(0)
29
30 /* IPC command for accessing SoC registers */
31 #define PMC_IPC_CMD_SOC_REG_ACC 0xAA
32 #define PMC_IPC_CMD_SUBCMD_SOC_REG_RD 0x00
33 #define PMC_IPC_CMD_REGID_SOC_QDF 0x03
34
35 static struct chipset_power_state power_state;
36
37 /* List of Minimum Assertion durations in microseconds */
38 enum min_assert_dur {
39 MinAssertDur0s = 0,
40 MinAssertDur60us = 60,
41 MinAssertDur1ms = 1000,
42 MinAssertDur50ms = 50000,
43 MinAssertDur98ms = 98000,
44 MinAssertDur500ms = 500000,
45 MinAssertDur1s = 1000000,
46 MinAssertDur2s = 2000000,
47 MinAssertDur3s = 3000000,
48 MinAssertDur4s = 4000000,
49 };
50
51 /* Signal Assertion duration values */
52 struct cfg_assert_dur {
53 /* Minimum assertion duration of SLP_A signal */
54 enum min_assert_dur slp_a;
55
56 /* Minimum assertion duration of SLP_4 signal */
57 enum min_assert_dur slp_s4;
58
59 /* Minimum assertion duration of SLP_3 signal */
60 enum min_assert_dur slp_s3;
61
62 /* PCH PM Power Cycle duration */
63 enum min_assert_dur pm_pwr_cyc_dur;
64 };
65
66 /* Default value of PchPmPwrCycDur */
67 #define PCH_PM_PWR_CYC_DUR 0
68
69 struct chipset_power_state *pmc_get_power_state(void)
70 {
71 struct chipset_power_state *ptr = NULL;
72
73 if (ENV_HAS_CBMEM)
74 ptr = acpi_get_pm_state();
75
76 /* cbmem is online but ptr is not populated yet */
77 if (ptr == NULL && !(ENV_RAMSTAGE || ENV_POSTCAR))
78 return &power_state;
79
80 return ptr;
81 }
82
83 static void migrate_power_state(int is_recovery)
84 {
85 struct chipset_power_state *ps_cbmem;
86
87 ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
88
89 if (ps_cbmem == NULL) {
90 printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
91 return;
92 }
93 memcpy(ps_cbmem, &power_state, sizeof(*ps_cbmem));
94 }
95 CBMEM_CREATION_HOOK(migrate_power_state);
96
97 static void print_num_status_bits(int num_bits, uint32_t status,
98 const char *const *bit_names)
99 {
100 int i;
101
102 if (!status)
103 return;
104
105 for (i = num_bits - 1; i >= 0; i--) {
106 if (status & (1 << i)) {
107 if (bit_names[i])
108 printk(BIOS_DEBUG, "%s ", bit_names[i]);
109 else
110 printk(BIOS_DEBUG, "BIT%d ", i);
111 }
112 }
113 }
114
115 __weak uint32_t soc_get_smi_status(uint32_t generic_sts)
116 {
117 return generic_sts;
118 }
119
120 int acpi_get_sleep_type(void)
121 {
122 struct chipset_power_state *ps;
123 int prev_sleep_state = ACPI_S0;
124
125 ps = pmc_get_power_state();
126 if (ps)
127 prev_sleep_state = ps->prev_sleep_state;
128
129 return prev_sleep_state;
130 }
131
132 static uint32_t pmc_reset_smi_status(void)
133 {
134 uint32_t smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
135 outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
136
137 return soc_get_smi_status(smi_sts);
138 }
139
140 static uint32_t print_smi_status(uint32_t smi_sts)
141 {
142 size_t array_size;
143 const char *const *smi_arr;
144
145 if (!smi_sts)
146 return 0;
147
148 printk(BIOS_DEBUG, "SMI_STS: ");
149
150 smi_arr = soc_smi_sts_array(&array_size);
151
152 print_num_status_bits(array_size, smi_sts, smi_arr);
153 printk(BIOS_DEBUG, "\n");
154
155 return smi_sts;
156 }
157
158 /*
159 * Update supplied events in PM1_EN register. This does not disable any already
160 * set events.
161 */
162 void pmc_update_pm1_enable(u16 events)
163 {
164 u16 pm1_en = pmc_read_pm1_enable();
165 pm1_en |= events;
166 pmc_enable_pm1(pm1_en);
167 }
168
169 /* Read events set in PM1_EN register. */
170 uint16_t pmc_read_pm1_enable(void)
171 {
172 return inw(ACPI_BASE_ADDRESS + PM1_EN);
173 }
174
175 uint32_t pmc_clear_smi_status(void)
176 {
177 uint32_t sts = pmc_reset_smi_status();
178
179 return print_smi_status(sts);
180 }
181
182 uint32_t pmc_get_smi_en(void)
183 {
184 return inl(ACPI_BASE_ADDRESS + SMI_EN);
185 }
186
187 void pmc_enable_smi(uint32_t mask)
188 {
189 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
190 smi_en |= mask;
191 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
192 }
193
194 void pmc_disable_smi(uint32_t mask)
195 {
196 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
197 smi_en &= ~mask;
198 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
199 }
200
201 /* PM1 */
202 void pmc_enable_pm1(uint16_t events)
203 {
204 outw(events, ACPI_BASE_ADDRESS + PM1_EN);
205 }
206
207 uint32_t pmc_read_pm1_control(void)
208 {
209 return inl(ACPI_BASE_ADDRESS + PM1_CNT);
210 }
211
212 void pmc_write_pm1_control(uint32_t pm1_cnt)
213 {
214 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
215 }
216
217 void pmc_enable_pm1_control(uint32_t mask)
218 {
219 uint32_t pm1_cnt = pmc_read_pm1_control();
220 pm1_cnt |= mask;
221 pmc_write_pm1_control(pm1_cnt);
222 }
223
224 void pmc_disable_pm1_control(uint32_t mask)
225 {
226 uint32_t pm1_cnt = pmc_read_pm1_control();
227 pm1_cnt &= ~mask;
228 pmc_write_pm1_control(pm1_cnt);
229 }
230
231 static uint16_t reset_pm1_status(void)
232 {
233 uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
234 outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
235 return pm1_sts;
236 }
237
238 static uint16_t print_pm1_status(uint16_t pm1_sts)
239 {
240 static const char *const pm1_sts_bits[] = {
241 [0] = "TMROF",
242 [5] = "GBL",
243 [8] = "PWRBTN",
244 [10] = "RTC",
245 [11] = "PRBTNOR",
246 [13] = "USB",
247 [14] = "PCIEXPWAK",
248 [15] = "WAK",
249 };
250
251 if (!pm1_sts)
252 return 0;
253
254 printk(BIOS_DEBUG, "PM1_STS: ");
255 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
256 printk(BIOS_DEBUG, "\n");
257
258 return pm1_sts;
259 }
260
261 uint16_t pmc_clear_pm1_status(void)
262 {
263 return print_pm1_status(reset_pm1_status());
264 }
265
266 /* TCO */
267
268 static uint32_t print_tco_status(uint32_t tco_sts)
269 {
270 size_t array_size;
271 const char *const *tco_arr;
272
273 if (!tco_sts)
274 return 0;
275
276 printk(BIOS_DEBUG, "TCO_STS: ");
277
278 tco_arr = soc_tco_sts_array(&array_size);
279
280 print_num_status_bits(array_size, tco_sts, tco_arr);
281 printk(BIOS_DEBUG, "\n");
282
283 return tco_sts;
284 }
285
286 uint32_t pmc_clear_tco_status(void)
287 {
288 return print_tco_status(tco_reset_status());
289 }
290
291 /* GPE */
292 static void pmc_enable_gpe(int gpe, uint32_t mask)
293 {
294 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
295 gpe0_en |= mask;
296 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
297 }
298
299 static void pmc_disable_gpe(int gpe, uint32_t mask)
300 {
301 uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
302 gpe0_en &= ~mask;
303 outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
304 }
305
306 void pmc_enable_std_gpe(uint32_t mask)
307 {
308 pmc_enable_gpe(GPE_STD, mask);
309 }
310
311 void pmc_disable_std_gpe(uint32_t mask)
312 {
313 pmc_disable_gpe(GPE_STD, mask);
314 }
315
316 void pmc_disable_all_gpe(void)
317 {
318 int i;
319 for (i = 0; i < GPE0_REG_MAX; i++)
320 pmc_disable_gpe(i, ~0);
321 }
322
323 /* Clear the gpio gpe0 status bits in ACPI registers */
324 static void pmc_clear_gpi_gpe_status(void)
325 {
326 int i;
327
328 for (i = 0; i < GPE0_REG_MAX; i++) {
329 /* This is reserved GPE block and specific to chipset */
330 if (i == GPE_STD)
331 continue;
332 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
333 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(i));
334 }
335 }
336
337 static uint32_t reset_std_gpe_status(void)
338 {
339 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
340 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
341 return gpe_sts;
342 }
343
344 static uint32_t print_std_gpe_sts(uint32_t gpe_sts)
345 {
346 size_t array_size;
347 const char *const *sts_arr;
348
349 if (!gpe_sts)
350 return gpe_sts;
351
352 printk(BIOS_DEBUG, "GPE0 STD STS: ");
353
354 sts_arr = soc_std_gpe_sts_array(&array_size);
355 print_num_status_bits(array_size, gpe_sts, sts_arr);
356 printk(BIOS_DEBUG, "\n");
357
358 return gpe_sts;
359 }
360
361 static void pmc_clear_std_gpe_status(void)
362 {
363 print_std_gpe_sts(reset_std_gpe_status());
364 }
365
366 void pmc_clear_all_gpe_status(void)
367 {
368 pmc_clear_std_gpe_status();
369 pmc_clear_gpi_gpe_status();
370 }
371
372 __weak
373 void soc_clear_pm_registers(uintptr_t pmc_bar)
374 {
375 }
376
377 void pmc_or_mmio32(uint32_t offset, uint32_t ormask)
378 {
379 uint32_t reg;
380 uintptr_t pmc_bar;
381
382 pmc_bar = soc_read_pmc_base();
383 reg = read32p(pmc_bar + offset);
384 reg |= ormask;
385 write32p(pmc_bar + offset, reg);
386 }
387
388 void pmc_clear_prsts(void)
389 {
390 uint32_t prsts;
391 uintptr_t pmc_bar;
392
393 /* Read PMC base address from soc */
394 pmc_bar = soc_read_pmc_base();
395
396 prsts = read32p(pmc_bar + PRSTS);
397 write32p(pmc_bar + PRSTS, prsts);
398
399 soc_clear_pm_registers(pmc_bar);
400 }
401
402 __weak
403 int soc_prev_sleep_state(const struct chipset_power_state *ps,
404 int prev_sleep_state)
405 {
406 return prev_sleep_state;
407 }
408
409 /*
410 * Returns prev_sleep_state and also prints all power management registers.
411 * Calls soc_prev_sleep_state which may be implemented by SOC.
412 */
413 static int pmc_prev_sleep_state(const struct chipset_power_state *ps)
414 {
415 /* Default to S0. */
416 int prev_sleep_state = ACPI_S0;
417
418 if (ps->pm1_sts & WAK_STS) {
419 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
420 case ACPI_S3:
421 if (CONFIG(HAVE_ACPI_RESUME))
422 prev_sleep_state = ACPI_S3;
423 break;
424 case ACPI_S4:
425 prev_sleep_state = ACPI_S4;
426 break;
427 case ACPI_S5:
428 prev_sleep_state = ACPI_S5;
429 break;
430 }
431
432 /* Clear SLP_TYP. */
433 pmc_write_pm1_control(ps->pm1_cnt & ~(SLP_TYP));
434
435 /* Clear PM1_STATUS */
436 pmc_clear_pm1_status();
437 }
438
439 prev_sleep_state = soc_prev_sleep_state(ps, prev_sleep_state);
440
441 /* Clear PMC PMCON_x register power failure status bits. */
442 pmc_clear_pmcon_pwr_failure_sts();
443
444 return prev_sleep_state;
445 }
446
447 static bool pmc_get_global_reset_sts_mmio(void)
448 {
449 uint8_t *addr = pmc_mmio_regs();
450
451 return !!(read32p((uintptr_t)(addr + GEN_PMCON_A)) & GBL_RST_STS);
452 }
453
454 static bool pmc_get_global_reset_sts_pci(void)
455 {
456 #if defined(__SIMPLE_DEVICE__)
457 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(PCH_DEVFN_PMC), PCI_FUNC(PCH_DEVFN_PMC));
458 #else
459 struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
460 if (!dev)
461 return false;
462 #endif
463
464 return !!(pci_read_config32(dev, GEN_PMCON_A) & GBL_RST_STS);
465 }
466
467 static bool pmc_get_global_reset_sts(void)
468 {
469 if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
470 return pmc_get_global_reset_sts_mmio();
471 else
472 return pmc_get_global_reset_sts_pci();
473 }
474
475 void pmc_fill_pm_reg_info(struct chipset_power_state *ps)
476 {
477 int i;
478
479 memset(ps, 0, sizeof(*ps));
480
481 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
482 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
483 ps->pm1_cnt = pmc_read_pm1_control();
484
485 /*
486 * Before system memory initialization, AP firmware should check the wake status
487 * to determine if Intel CSE has reset the system and system sleep state (PM1_CNT)
488 * holds a valid sleep entry?
489 *
490 * If not, then AP FW should force to take a S5 exit path (by programming the PM1_CNT)
491 * register as per PM register initialization requirement for CSE.
492 */
493 if (pmc_get_global_reset_sts() && (ps->pm1_sts & WAK_STS) && !(ps->pm1_cnt & SLP_TYP)) {
494 printk(BIOS_DEBUG, "Enforcing the S5 exit path\n");
495 uint32_t pm1_cnt_slp_type = SLP_TYP_S5 << SLP_TYP_SHIFT;
496 ps->pm1_cnt |= pm1_cnt_slp_type;
497 pmc_enable_pm1_control(pm1_cnt_slp_type);
498 }
499
500 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
501 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
502
503 for (i = 0; i < GPE0_REG_MAX; i++) {
504 ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
505 ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
506 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
507 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
508 }
509
510 soc_fill_power_state(ps);
511 }
512
513 /* Reads and prints ACPI specific PM registers */
514 int pmc_fill_power_state(struct chipset_power_state *ps)
515 {
516 /* Define the sleep state string */
517 static const char * const acpi_sleep_states[] = {
518 [ACPI_S0] = "S0",
519 [ACPI_S1] = "S1",
520 [ACPI_S3] = "S3",
521 [ACPI_S4] = "S4",
522 [ACPI_S5] = "S5",
523 };
524
525 pmc_fill_pm_reg_info(ps);
526
527 ps->prev_sleep_state = pmc_prev_sleep_state(ps);
528
529 if (ps->prev_sleep_state < ARRAY_SIZE(acpi_sleep_states) &&
530 acpi_sleep_states[ps->prev_sleep_state] != NULL)
531 printk(BIOS_DEBUG, "prev_sleep_state %d (%s)\n", ps->prev_sleep_state,
532 acpi_sleep_states[ps->prev_sleep_state]);
533 else
534 printk(BIOS_DEBUG, "prev_sleep_state %d (unknown state)\n", ps->prev_sleep_state);
535
536 return ps->prev_sleep_state;
537 }
538
539 #if CONFIG(PMC_GLOBAL_RESET_ENABLE_LOCK)
540 void pmc_global_reset_disable_and_lock(void)
541 {
542 uint32_t *etr = soc_pmc_etr_addr();
543 uint32_t reg;
544
545 reg = read32(etr);
546 reg = (reg & ~CF9_GLB_RST) | CF9_LOCK;
547 write32(etr, reg);
548 }
549
550 void pmc_global_reset_enable(bool enable)
551 {
552 uint32_t *etr = soc_pmc_etr_addr();
553 uint32_t reg;
554
555 reg = read32(etr);
556 reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
557 write32(etr, reg);
558 }
559 #endif // CONFIG_PMC_GLOBAL_RESET_ENABLE_LOCK
560
561 int platform_is_resuming(void)
562 {
563 /* Read power state from PMC data structure */
564 if (ENV_RAMSTAGE)
565 return acpi_get_sleep_type() == ACPI_S3;
566
567 /* Read power state from PMC ABASE */
568 if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
569 return 0;
570
571 return acpi_sleep_from_pm1(pmc_read_pm1_control()) == ACPI_S3;
572 }
573
574 /* Read and clear GPE status (defined in acpi/acpi.h) */
575 int acpi_get_gpe(int gpe)
576 {
577 int bank;
578 uint32_t mask, sts;
579 struct stopwatch sw;
580 int rc = 0;
581
582 if (gpe < 0 || gpe > GPE_MAX)
583 return -1;
584
585 bank = gpe / 32;
586 mask = 1 << (gpe % 32);
587
588 /* Wait up to 1ms for GPE status to clear */
589 stopwatch_init_msecs_expire(&sw, 1);
590 do {
591 if (stopwatch_expired(&sw))
592 return rc;
593
594 sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(bank));
595 if (sts & mask) {
596 outl(mask, ACPI_BASE_ADDRESS + GPE0_STS(bank));
597 rc = 1;
598 }
599 } while (sts & mask);
600
601 return rc;
602 }
603
604 /*
605 * The PM1 control is set to S5 when vboot requests a reboot because the power
606 * state code above may not have collected its data yet. Therefore, set it to
607 * S5 when vboot requests a reboot. That's necessary if vboot fails in the
608 * resume path and requests a reboot. This prevents a reboot loop where the
609 * error is continually hit on the failing vboot resume path.
610 */
611 void vboot_platform_prepare_reboot(void)
612 {
613 uint32_t pm1_cnt;
614 pm1_cnt = (pmc_read_pm1_control() & ~(SLP_TYP)) |
615 (SLP_TYP_S5 << SLP_TYP_SHIFT);
616 pmc_write_pm1_control(pm1_cnt);
617 }
618
619 /* Helper function to perform poweroff operation using PMC chipset register. */
620 static void pmc_control_poweroff(void)
621 {
622 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
623
624 /*
625 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
626 * to transition to S5 state. If halt is called in SMM, then it prevents
627 * the SMI handler from being triggered and system never enters S5.
628 */
629 if (!ENV_SMM)
630 halt();
631 }
632
633 void poweroff(void)
634 {
635 if (!ENV_ROMSTAGE_OR_BEFORE) {
636 pmc_control_poweroff();
637 } else if (CONFIG(HAVE_EARLY_POWEROFF_SUPPORT)) {
638 platform_do_early_poweroff();
639 } else {
640 printk(BIOS_EMERG, "This platform cannot be powered off until the silicon"
641 " initialization is complete, hanging!\n");
642 halt();
643 }
644 }
645
646 void pmc_gpe_init(void)
647 {
648 uint32_t gpio_cfg = 0;
649 uint32_t gpio_cfg_reg;
650 uint8_t dw0 = 0, dw1 = 0, dw2 = 0;
651
652 /* Read PMC base address from soc. This is implemented in soc */
653 uintptr_t pmc_bar = soc_read_pmc_base();
654
655 /*
656 * Get the dwX values for pmc gpe settings.
657 */
658 soc_get_gpi_gpe_configs(&dw0, &dw1, &dw2);
659
660 const uint32_t gpio_cfg_mask =
661 (GPE0_DWX_MASK << GPE0_DW_SHIFT(0)) |
662 (GPE0_DWX_MASK << GPE0_DW_SHIFT(1)) |
663 (GPE0_DWX_MASK << GPE0_DW_SHIFT(2));
664
665 /* Making sure that bad values don't bleed into the other fields */
666 dw0 &= GPE0_DWX_MASK;
667 dw1 &= GPE0_DWX_MASK;
668 dw2 &= GPE0_DWX_MASK;
669
670 /*
671 * Route the GPIOs to the GPE0 block. Determine that all values
672 * are different, and if they aren't use the reset values.
673 */
674 if (dw0 == dw1 || dw1 == dw2 || dw0 == dw2) {
675 printk(BIOS_WARNING, "PMC: Duplicate GPE DW register values detected; "
676 "using default GPE route from MISCCFG register\n");
677 gpio_cfg = read32p(pmc_bar + GPIO_GPE_CFG);
678
679 dw0 = (gpio_cfg >> GPE0_DW_SHIFT(0)) & GPE0_DWX_MASK;
680 dw1 = (gpio_cfg >> GPE0_DW_SHIFT(1)) & GPE0_DWX_MASK;
681 dw2 = (gpio_cfg >> GPE0_DW_SHIFT(2)) & GPE0_DWX_MASK;
682 } else {
683 gpio_cfg |= (uint32_t)dw0 << GPE0_DW_SHIFT(0);
684 gpio_cfg |= (uint32_t)dw1 << GPE0_DW_SHIFT(1);
685 gpio_cfg |= (uint32_t)dw2 << GPE0_DW_SHIFT(2);
686 }
687
688 gpio_cfg_reg = read32p(pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask;
689 gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask;
690
691 write32p(pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg);
692
693 /* Set the routes in the GPIO communities as well. */
694 gpio_route_gpe(dw0, dw1, dw2);
695 }
696
697 static void pmc_clear_pmcon_pwr_failure_sts_mmio(void)
698 {
699 uint8_t *addr = pmc_mmio_regs();
700
701 /*
702 * Clear PMC GEN_PMCON_A register power failure status bits:
703 * SUS_PWR_FLR, PWR_FLR bits
704 * while retaining MS4V write-1-to-clear bit
705 *
706 * Note: clearing `GBL_RST_STS` bit earlier than FSP-M/MRC having an adverse effect
707 * on the PMC sleep type register which results in calculating wrong
708 * `prev_sleep_state` post a global reset, hence, just clearing the power failure
709 * status bits rather than clearing the complete PMC PMCON_A register.
710 */
711 clrbits32((addr + GEN_PMCON_A), (MS4V | GBL_RST_STS));
712 }
713
714 static void pmc_clear_pmcon_pwr_failure_sts_pci(void)
715 {
716 #if defined(__SIMPLE_DEVICE__)
717 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(PCH_DEVFN_PMC), PCI_FUNC(PCH_DEVFN_PMC));
718 #else
719 struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
720 if (!dev)
721 return;
722 #endif
723
724 pci_or_config32(dev, GEN_PMCON_B, (SUS_PWR_FLR | PWR_FLR));
725 }
726
727 /*
728 * Clear PMC GEN_PMCON_X register power failure status bits:
729 * SUS_PWR_FLR, PWR_FLR bits (keep the other bits intact)
730 */
731 void pmc_clear_pmcon_pwr_failure_sts(void)
732 {
733 if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
734 pmc_clear_pmcon_pwr_failure_sts_mmio();
735 else
736 pmc_clear_pmcon_pwr_failure_sts_pci();
737 }
738
739 #if ENV_RAMSTAGE
740 static void pmc_clear_pmcon_sts_mmio(void)
741 {
742 uint8_t *addr = pmc_mmio_regs();
743
744 clrbits32((addr + GEN_PMCON_A), MS4V);
745 }
746
747 static void pmc_clear_pmcon_sts_pci(void)
748 {
749 struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
750 if (!dev)
751 return;
752
753 pci_and_config32(dev, GEN_PMCON_A, ~MS4V);
754 }
755
756 /*
757 * Clear PMC GEN_PMCON_A register status bits:
758 * SUS_PWR_FLR, GBL_RST_STS, HOST_RST_STS, PWR_FLR bits
759 * while retaining MS4V write-1-to-clear bit
760 */
761 void pmc_clear_pmcon_sts(void)
762 {
763 /*
764 * Accessing PMC GEN_PMCON_A register differs between different Intel chipsets.
765 * Typically, there are two possible ways to perform GEN_PMCON_A register programming
766 * (like `pmc_clear_pmcon_sts()`) as:
767 * 1. Using PCI configuration space when GEN_PMCON_A is a PCI configuration register.
768 * 2. Using MMIO access when GEN_PMCON_A is a memory mapped register.
769 */
770 if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
771 pmc_clear_pmcon_sts_mmio();
772 else
773 pmc_clear_pmcon_sts_pci();
774 }
775 #endif
776
777 void pmc_set_power_failure_state(const bool target_on)
778 {
779 const unsigned int state = get_uint_option("power_on_after_fail",
780 CONFIG_MAINBOARD_POWER_FAILURE_STATE);
781
782 /*
783 * On the shutdown path (target_on == false), we only need to
784 * update the register for MAINBOARD_POWER_STATE_PREVIOUS. For
785 * all other cases, we don't write the register to avoid clob-
786 * bering the value set on the boot path. This is necessary,
787 * for instance, when we can't access the option backend in SMM.
788 */
789
790 switch (state) {
791 case MAINBOARD_POWER_STATE_OFF:
792 if (!target_on)
793 break;
794 printk(BIOS_INFO, "Set power off after power failure.\n");
795 pmc_soc_set_afterg3_en(false);
796 break;
797 case MAINBOARD_POWER_STATE_ON:
798 if (!target_on)
799 break;
800 printk(BIOS_INFO, "Set power on after power failure.\n");
801 pmc_soc_set_afterg3_en(true);
802 break;
803 case MAINBOARD_POWER_STATE_PREVIOUS:
804 printk(BIOS_INFO, "Keep power state after power failure.\n");
805 pmc_soc_set_afterg3_en(target_on);
806 break;
807 default:
808 printk(BIOS_WARNING, "Unknown power-failure state: %d\n", state);
809 break;
810 }
811 }
812
813 /* This function returns the highest assertion duration of the SLP_Sx assertion widths */
814 static enum min_assert_dur get_high_assert_width(const struct cfg_assert_dur *cfg_assert_dur)
815 {
816 enum min_assert_dur max_assert_dur = cfg_assert_dur->slp_s4;
817
818 if (max_assert_dur < cfg_assert_dur->slp_s3)
819 max_assert_dur = cfg_assert_dur->slp_s3;
820
821 if (max_assert_dur < cfg_assert_dur->slp_a)
822 max_assert_dur = cfg_assert_dur->slp_a;
823
824 return max_assert_dur;
825 }
826
827 /* This function converts assertion durations from register-encoded to microseconds */
828 static void get_min_assert_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
829 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur,
830 struct cfg_assert_dur *cfg_assert_dur)
831 {
832 /*
833 * Ensure slp_x_dur_list[] elements in the devicetree config are in sync with
834 * FSP encoded values.
835 */
836
837 /* slp_s4_assert_dur_list : 1s, 1s(default), 2s, 3s, 4s */
838 const enum min_assert_dur slp_s4_assert_dur_list[] = {
839 MinAssertDur1s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
840 };
841
842 /* slp_s3_assert_dur_list: 50ms, 60us, 1ms, 50ms (Default), 2s */
843 const enum min_assert_dur slp_s3_assert_dur_list[] = {
844 MinAssertDur50ms, MinAssertDur60us, MinAssertDur1ms, MinAssertDur50ms,
845 MinAssertDur2s
846 };
847
848 /* slp_a_assert_dur_list: 2s, 0s, 4s, 98ms, 2s(Default) */
849 const enum min_assert_dur slp_a_assert_dur_list[] = {
850 MinAssertDur2s, MinAssertDur0s, MinAssertDur4s, MinAssertDur98ms, MinAssertDur2s
851 };
852
853 /* pm_pwr_cyc_dur_list: 4s(Default), 1s, 2s, 3s, 4s */
854 const enum min_assert_dur pm_pwr_cyc_dur_list[] = {
855 MinAssertDur4s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
856 };
857
858 /* Get signal assertion width */
859 if (slp_s4_min_assert < ARRAY_SIZE(slp_s4_assert_dur_list))
860 cfg_assert_dur->slp_s4 = slp_s4_assert_dur_list[slp_s4_min_assert];
861
862 if (slp_s3_min_assert < ARRAY_SIZE(slp_s3_assert_dur_list))
863 cfg_assert_dur->slp_s3 = slp_s3_assert_dur_list[slp_s3_min_assert];
864
865 if (slp_a_min_assert < ARRAY_SIZE(slp_a_assert_dur_list))
866 cfg_assert_dur->slp_a = slp_a_assert_dur_list[slp_a_min_assert];
867
868 if (pm_pwr_cyc_dur < ARRAY_SIZE(pm_pwr_cyc_dur_list))
869 cfg_assert_dur->pm_pwr_cyc_dur = pm_pwr_cyc_dur_list[pm_pwr_cyc_dur];
870 }
871
872 /*
873 * This function ensures that the duration programmed in the PchPmPwrCycDur will never be
874 * smaller than the SLP_Sx assertion widths.
875 * If the pm_pwr_cyc_dur is less than any of the SLP_Sx assertion widths then it returns the
876 * default value PCH_PM_PWR_CYC_DUR.
877 */
878 uint8_t get_pm_pwr_cyc_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
879 uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur)
880 {
881 /* Set default values for the minimum assertion duration */
882 struct cfg_assert_dur cfg_assert_dur = {
883 .slp_a = MinAssertDur2s,
884 .slp_s4 = MinAssertDur1s,
885 .slp_s3 = MinAssertDur50ms,
886 .pm_pwr_cyc_dur = MinAssertDur4s
887 };
888
889 enum min_assert_dur high_assert_width;
890
891 /* Convert assertion durations from register-encoded to microseconds */
892 get_min_assert_dur(slp_s4_min_assert, slp_s3_min_assert, slp_a_min_assert,
893 pm_pwr_cyc_dur, &cfg_assert_dur);
894
895 /* Get the highest assertion duration among PCH EDS specified signals for pwr_cyc_dur */
896 high_assert_width = get_high_assert_width(&cfg_assert_dur);
897
898 if (cfg_assert_dur.pm_pwr_cyc_dur >= high_assert_width)
899 return pm_pwr_cyc_dur;
900
901 printk(BIOS_DEBUG,
902 "Set PmPwrCycDur to 4s as configured PmPwrCycDur (%d) violates PCH EDS "
903 "spec\n", pm_pwr_cyc_dur);
904
905 return PCH_PM_PWR_CYC_DUR;
906 }
907
908 void pmc_set_acpi_mode(void)
909 {
910 if (!CONFIG(NO_SMM) && !acpi_is_wakeup_s3()) {
911 apm_control(APM_CNT_ACPI_DISABLE);
912 }
913 }
914
915 enum pch_pmc_xtal pmc_get_xtal_freq(void)
916 {
917 if (!CONFIG(SOC_INTEL_COMMON_BLOCK_PMC_EPOC))
918 dead_code();
919
920 uint32_t xtal_freq = 0;
921 const uint32_t epoc = read32p(soc_read_pmc_base() + PCH_PMC_EPOC);
922
923 /* XTAL frequency in bits 21, 20, 17 */
924 xtal_freq |= !!(epoc & (1 << 21)) << 2;
925 xtal_freq |= !!(epoc & (1 << 20)) << 1;
926 xtal_freq |= !!(epoc & (1 << 17)) << 0;
927 switch (xtal_freq) {
928 case 0:
929 return XTAL_24_MHZ;
930 case 1:
931 return XTAL_19_2_MHZ;
932 case 2:
933 return XTAL_38_4_MHZ;
934 default:
935 printk(BIOS_ERR, "Unknown EPOC XTAL frequency setting %u\n", xtal_freq);
936 return XTAL_UNKNOWN_FREQ;
937 }
938 }
939
940 void pmc_send_bios_reset_pci_enum_done(void)
941 {
942 struct pmc_ipc_buffer req = { 0 };
943 struct pmc_ipc_buffer rsp;
944 uint32_t cmd;
945
946 req.buf[0] = PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM;
947 cmd = pmc_make_ipc_cmd(PMC_IPC_BIOS_RST_COMPLETE,
948 PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE, 0);
949 if (pmc_send_ipc_cmd(cmd, &req, &rsp) != CB_SUCCESS)
950 printk(BIOS_ERR, "PMC: Failed sending PCI Enumeration Done Command\n");
951 }
952
953 char *retrieve_soc_qdf_info_via_pmc_ipc(void)
954 {
955 struct pmc_ipc_buffer req = { 0 };
956 struct pmc_ipc_buffer rsp;
957 uint32_t cmd_reg;
958 int r;
959 static char qdf_info[5] = { 0 };
960
961 if (!CONFIG(SOC_QDF_DYNAMIC_READ_PMC))
962 return NULL;
963
964 req.buf[0] = PMC_IPC_CMD_REGID_SOC_QDF;
965 cmd_reg = pmc_make_ipc_cmd(PMC_IPC_CMD_SOC_REG_ACC,
966 PMC_IPC_CMD_SUBCMD_SOC_REG_RD,
967 PMC_IPC_BUF_COUNT);
968
969 r = pmc_send_ipc_cmd(cmd_reg, &req, &rsp);
970
971 if (r < 0 || rsp.buf[0] == 0) {
972 printk(BIOS_ERR, "%s: pmc_send_ipc_cmd failed or QDF not available.\n",
973 __func__);
974 return NULL;
975 }
976
977 qdf_info[0] = ((rsp.buf[0] >> 24) & 0xFF);
978 qdf_info[1] = ((rsp.buf[0] >> 16) & 0xFF);
979 qdf_info[2] = ((rsp.buf[0] >> 8) & 0xFF);
980 qdf_info[3] = (rsp.buf[0] & 0xFF);
981 qdf_info[4] = '\0';
982
983 return qdf_info;
984 }
985
986 /*
987 * This function reads and prints SoC QDF information using PMC interface
988 * if SOC_QDF_DYNAMIC_READ_PMC config is enabled.
989 */
990 void pmc_dump_soc_qdf_info(void)
991 {
992 char *qdf = retrieve_soc_qdf_info_via_pmc_ipc();
993
994 if (qdf != NULL)
995 printk(BIOS_INFO, "SoC QDF: %s\n", qdf);
996 }