Debian Codegraph @

src/soc/intel/xeon_sp/pcu1.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <bootstate.h>
4 #include <delay.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <soc/acpi.h>
8 #include <soc/chip_common.h>
9 #include <soc/pci_devs.h>
10 #include <soc/pcu.h>
11 #include <soc/ramstage.h>
12 #include <soc/util.h>
13 #include <timer.h>
14
15 /* return true if command timed out else false */
16 static bool wait_for_bios_cmd_cpl(struct device *pcu1, uint32_t reg, uint32_t mask,
17 uint32_t target)
18 {
19 const uint32_t max_delay = 5000; /* 5 seconds max */
20 const uint32_t step_delay = 50; /* 50 us */
21 struct stopwatch sw;
22
23 stopwatch_init_msecs_expire(&sw, max_delay);
24 while ((pci_read_config32(pcu1, reg) & mask) != target) {
25 udelay(step_delay);
26 if (stopwatch_expired(&sw)) {
27 printk(BIOS_ERR, "%s timed out for dev: %s, reg: 0x%x, "
28 "mask: 0x%x, target: 0x%x\n",
29 __func__, dev_path(pcu1), reg, mask, target);
30 return true; /* timedout */
31 }
32 }
33 return false; /* successful */
34 }
35
36 /* return true if command timed out else false */
37 static bool write_bios_mailbox_cmd(struct device *pcu1, uint32_t command, uint32_t data)
38 {
39 /* verify bios is not in busy state */
40 if (wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG, BIOS_MB_RUN_BUSY_MASK, 0))
41 return true; /* timed out */
42
43 /* write data to data register */
44 printk(BIOS_SPEW, "%s - pci_write_config32 reg: 0x%x, data: 0x%x\n", __func__,
45 PCU_CR1_BIOS_MB_DATA_REG, data);
46
47 pci_write_config32(pcu1, PCU_CR1_BIOS_MB_DATA_REG, data);
48
49 /* write the command */
50 printk(BIOS_SPEW, "%s - pci_write_config32 reg: 0x%x, data: 0x%lx\n", __func__,
51 PCU_CR1_BIOS_MB_INTERFACE_REG, command | BIOS_MB_RUN_BUSY_MASK);
52
53 pci_write_config32(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG,
54 command | BIOS_MB_RUN_BUSY_MASK);
55
56 /* wait for completion or time out*/
57 return wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG,
58 BIOS_MB_RUN_BUSY_MASK, 0);
59 }
60
61 /* return true if command timed out else false */
62 static bool set_bios_reset_cpl_for_package(struct device *pcu1,
63 uint32_t rst_cpl_mask,
64 uint32_t pcode_init_mask,
65 uint32_t val)
66 {
67 /* update BIOS RESET completion bit */
68 pci_update_config32(pcu1, PCU_CR1_BIOS_RESET_CPL_REG, ~rst_cpl_mask, val);
69
70 /* wait for PCU ack */
71 return wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_RESET_CPL_REG,
72 pcode_init_mask, pcode_init_mask);
73 }
74
75 static void set_bios_init_completion_for_package(uint32_t socket)
76 {
77 struct device *pcu1 = dev_find_device_on_socket(socket, PCI_VID_INTEL, PCU_CR1_DEVID);
78 bool timedout;
79
80 if (!pcu1)
81 die("Failed to locate PCU PCI device\n");
82
83 /* update RST_CPL3, PCODE_INIT_DONE3 */
84 timedout = set_bios_reset_cpl_for_package(pcu1, RST_CPL3_MASK,
85 PCODE_INIT_DONE3_MASK, RST_CPL3_MASK);
86 if (timedout)
87 die("BIOS RESET CPL3 timed out.\n");
88
89 /* update RST_CPL4, PCODE_INIT_DONE4 */
90 timedout = set_bios_reset_cpl_for_package(pcu1, RST_CPL4_MASK,
91 PCODE_INIT_DONE4_MASK, RST_CPL4_MASK);
92 if (timedout)
93 die("BIOS RESET CPL4 timed out.\n");
94 }
95
96 static void set_bios_init_completion(void *unused)
97 {
98 uint32_t sbsp_socket_id = 0;
99
100 /*
101 * According to the BIOS Writer's Guide, the SBSP must be the last socket
102 * to receive the BIOS init completion message. So, we send it to all non-SBSP
103 * sockets first.
104 */
105 for (uint32_t socket = 0; socket < CONFIG_MAX_SOCKET; ++socket) {
106 if (!soc_cpu_is_enabled(socket))
107 continue;
108 if (socket == sbsp_socket_id)
109 continue;
110 set_bios_init_completion_for_package(socket);
111 }
112
113 /* And finally, take care of the SBSP */
114 set_bios_init_completion_for_package(sbsp_socket_id);
115 }
116
117 /* Set BIOS INIT completion after locking registers */
118 BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, set_bios_init_completion, NULL);
119 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, set_bios_init_completion, NULL);
120
121 static void pcu1_init(struct device *dev)
122 {
123 u32 data;
124 bool timedout;
125
126 printk(BIOS_INFO, "%s: init registers\n", dev_path(dev));
127
128 /* Make sure to issue BIOS_CMD_WRITE_PCU_MISC_CFG at least once */
129 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_READ_PCU_MISC_CFG, 0);
130 if (timedout) {
131 /* 2nd try */
132 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_READ_PCU_MISC_CFG, 0);
133 if (timedout)
134 die("BIOS PCU Misc Config Read timed out.\n");
135 }
136
137 data = pci_read_config32(dev, PCU_CR1_BIOS_MB_DATA_REG);
138 printk(BIOS_SPEW, "%s - pci_read_config32 reg: 0x%x, data: 0x%x\n",
139 __func__, PCU_CR1_BIOS_MB_DATA_REG, data);
140
141 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_WRITE_PCU_MISC_CFG, data);
142 if (timedout)
143 die("BIOS PCU Misc Config Write timed out.\n");
144
145 if (CONFIG(SOC_INTEL_SKYLAKE_SP)) {
146 /* clear bits 27:31 - FSP sets this with 0x7 which needs to be cleared */
147 pci_and_config32(dev, PCU_CR1_SAPMCTL, 0xfffffff);
148 }
149 }
150
151 static void pcu1_final(struct device *dev)
152 {
153 printk(BIOS_INFO, "%s: locking registers\n", dev_path(dev));
154 if (CONFIG(SOC_INTEL_SKYLAKE_SP) || CONFIG(SOC_INTEL_COOPERLAKE_SP)) {
155 pci_or_config32(dev, PCU_CR1_SAPMCTL, SAPMCTL_LOCK_MASK);
156 }
157 pci_or_config32(dev, PCU_CR1_DESIRED_CORES_CFG2_REG,
158 PCU_CR1_DESIRED_CORES_CFG2_REG_LOCK_MASK);
159 }
160
161 static struct device_operations pcu1_ops = {
162 .read_resources = pci_dev_read_resources,
163 .set_resources = pci_dev_set_resources,
164 .enable_resources = pci_dev_enable_resources,
165 .init = pcu1_init,
166 .final = pcu1_final,
167 };
168
169 static const struct pci_driver pcu1_driver __pci_driver = {
170 .ops = &pcu1_ops,
171 .vendor = PCI_VID_INTEL,
172 .device = PCU_CR1_DEVID,
173 };