mb/google/brya/var/orisa: Update Type C DisplayPort HPD Configuration
[coreboot2.git] / src / soc / intel / xeon_sp / util.c
blob69f3c154529c287bec26f453ce199ab5391bae1a
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <commonlib/sort.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <device/device.h>
8 #include <device/pci.h>
9 #include <device/pci_ids.h>
10 #include <intelblocks/cfg.h>
11 #include <intelblocks/cpulib.h>
12 #include <intelblocks/p2sb.h>
13 #include <intelpch/lockdown.h>
14 #include <soc/chip_common.h>
15 #include <soc/pch_pci_devs.h>
16 #include <soc/pci_devs.h>
17 #include <soc/msr.h>
18 #include <soc/soc_util.h>
19 #include <soc/util.h>
20 #include <timer.h>
22 msr_t read_msr_ppin(void)
24 msr_t ppin = {0};
25 msr_t msr;
27 /* If MSR_PLATFORM_INFO PPIN_CAP is 0, PPIN capability is not supported */
28 msr = rdmsr(MSR_PLATFORM_INFO);
29 if ((msr.lo & MSR_PPIN_CAP) == 0) {
30 printk(BIOS_ERR, "MSR_PPIN_CAP is 0, PPIN is not supported\n");
31 return ppin;
34 /* Access to MSR_PPIN is permitted only if MSR_PPIN_CTL LOCK is 0 and ENABLE is 1 */
35 msr = rdmsr(MSR_PPIN_CTL);
36 if (msr.lo & MSR_PPIN_CTL_LOCK) {
37 printk(BIOS_ERR, "MSR_PPIN_CTL_LOCK is 1, PPIN access is not allowed\n");
38 return ppin;
41 if ((msr.lo & MSR_PPIN_CTL_ENABLE) == 0) {
42 /* Set MSR_PPIN_CTL ENABLE to 1 */
43 msr.lo |= MSR_PPIN_CTL_ENABLE;
44 wrmsr(MSR_PPIN_CTL, msr);
46 ppin = rdmsr(MSR_PPIN);
47 return ppin;
50 static unsigned int get_threads_per_package(void)
52 unsigned int core_count, thread_count;
53 cpu_read_topology(&core_count, &thread_count);
54 return thread_count;
57 int get_platform_thread_count(void)
59 return soc_get_num_cpus() * get_threads_per_package();
62 const IIO_UDS *get_iio_uds(void)
64 size_t hob_size;
65 static const IIO_UDS *hob;
66 const uint8_t fsp_hob_iio_universal_data_guid[16] = FSP_HOB_IIO_UNIVERSAL_DATA_GUID;
68 if (hob)
69 return hob;
71 hob = fsp_find_extension_hob_by_guid(fsp_hob_iio_universal_data_guid, &hob_size);
72 assert(hob && hob_size != 0);
73 return hob;
77 * Returns true if the CPU in the specified socket was found
78 * during QPI init, false otherwise.
80 bool soc_cpu_is_enabled(const size_t idx)
82 const IIO_UDS *hob = get_iio_uds();
83 assert(idx < CONFIG_MAX_SOCKET);
85 return hob->PlatformData.IIO_resource[idx].Valid;
88 unsigned int soc_get_num_cpus(void)
90 return get_iio_uds()->SystemStatus.numCpus;
93 unsigned int smbios_soc_get_max_sockets(void)
95 return soc_get_num_cpus();
98 union p2sb_bdf soc_get_hpet_bdf(void)
100 if (CONFIG(SOC_INTEL_COMMON_IBL_BASE)) {
101 union p2sb_bdf bdf = {
102 .bus = HPET_BUS_NUM,
103 .dev = HPET_DEV_NUM,
104 .fn = HPET0_FUNC_NUM
106 return bdf;
108 return p2sb_get_hpet_bdf();
111 union p2sb_bdf soc_get_ioapic_bdf(void)
113 if (CONFIG(SOC_INTEL_COMMON_IBL_BASE)) {
114 union p2sb_bdf bdf = {
115 .bus = PCH_IOAPIC_BUS_NUMBER,
116 .dev = PCH_IOAPIC_DEV_NUM,
117 .fn = PCH_IOAPIC_FUNC_NUM
119 return bdf;
121 return p2sb_get_ioapic_bdf();
124 #if ENV_RAMSTAGE /* Setting devtree variables is only allowed in ramstage. */
126 /* return true if command timed out else false */
127 static bool wait_for_bios_cmd_cpl(struct device *pcu1, uint32_t reg, uint32_t mask,
128 uint32_t target)
130 const uint32_t max_delay = 5000; /* 5 seconds max */
131 const uint32_t step_delay = 50; /* 50 us */
132 struct stopwatch sw;
134 stopwatch_init_msecs_expire(&sw, max_delay);
135 while ((pci_read_config32(pcu1, reg) & mask) != target) {
136 udelay(step_delay);
137 if (stopwatch_expired(&sw)) {
138 printk(BIOS_ERR, "%s timed out for dev: %s, reg: 0x%x, "
139 "mask: 0x%x, target: 0x%x\n",
140 __func__, dev_path(pcu1), reg, mask, target);
141 return true; /* timedout */
144 return false; /* successful */
147 /* return true if command timed out else false */
148 static bool write_bios_mailbox_cmd(struct device *pcu1, uint32_t command, uint32_t data)
150 /* verify bios is not in busy state */
151 if (wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG, BIOS_MB_RUN_BUSY_MASK, 0))
152 return true; /* timed out */
154 /* write data to data register */
155 printk(BIOS_SPEW, "%s - pci_write_config32 reg: 0x%x, data: 0x%x\n", __func__,
156 PCU_CR1_BIOS_MB_DATA_REG, data);
158 pci_write_config32(pcu1, PCU_CR1_BIOS_MB_DATA_REG, data);
160 /* write the command */
161 printk(BIOS_SPEW, "%s - pci_write_config32 reg: 0x%x, data: 0x%lx\n", __func__,
162 PCU_CR1_BIOS_MB_INTERFACE_REG, command | BIOS_MB_RUN_BUSY_MASK);
164 pci_write_config32(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG,
165 command | BIOS_MB_RUN_BUSY_MASK);
167 /* wait for completion or time out*/
168 return wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_MB_INTERFACE_REG,
169 BIOS_MB_RUN_BUSY_MASK, 0);
172 /* return true if command timed out else false */
173 static bool set_bios_reset_cpl_for_package(struct device *pcu1,
174 uint32_t rst_cpl_mask,
175 uint32_t pcode_init_mask,
176 uint32_t val)
178 /* update BIOS RESET completion bit */
179 pci_update_config32(pcu1, PCU_CR1_BIOS_RESET_CPL_REG, ~rst_cpl_mask, val);
181 /* wait for PCU ack */
182 return wait_for_bios_cmd_cpl(pcu1, PCU_CR1_BIOS_RESET_CPL_REG,
183 pcode_init_mask, pcode_init_mask);
186 static void set_bios_init_completion_for_package(uint32_t socket)
188 struct device *pcu0 = dev_find_device_on_socket(socket, PCI_VID_INTEL, PCU_CR0_DEVID);
189 struct device *pcu1 = dev_find_device_on_socket(socket, PCI_VID_INTEL, PCU_CR1_DEVID);
190 uint32_t data;
191 bool timedout;
193 if (!pcu0 || !pcu1)
194 die("Failed to locate PCU PCI device\n");
196 /* read PCU config */
197 timedout = write_bios_mailbox_cmd(pcu1, BIOS_CMD_READ_PCU_MISC_CFG, 0);
198 if (timedout) {
199 /* 2nd try */
200 timedout = write_bios_mailbox_cmd(pcu1, BIOS_CMD_READ_PCU_MISC_CFG, 0);
201 if (timedout)
202 die("BIOS PCU Misc Config Read timed out.\n");
204 /* Since the 1st try failed, we need to make sure PCU is in stable state */
205 data = pci_read_config32(pcu1, PCU_CR1_BIOS_MB_DATA_REG);
206 printk(BIOS_SPEW, "%s - pci_read_config32 reg: 0x%x, data: 0x%x\n",
207 __func__, PCU_CR1_BIOS_MB_DATA_REG, data);
208 timedout = write_bios_mailbox_cmd(pcu1, BIOS_CMD_WRITE_PCU_MISC_CFG, data);
209 if (timedout)
210 die("BIOS PCU Misc Config Write timed out.\n");
213 /* update RST_CPL3, PCODE_INIT_DONE3 */
214 timedout = set_bios_reset_cpl_for_package(pcu1, RST_CPL3_MASK,
215 PCODE_INIT_DONE3_MASK, RST_CPL3_MASK);
216 if (timedout)
217 die("BIOS RESET CPL3 timed out.\n");
219 /* Set PMAX_LOCK - must be set before RESET CPL4 */
220 data = pci_read_config32(pcu0, PCU_CR0_PMAX);
221 data |= PMAX_LOCK;
222 pci_write_config32(pcu0, PCU_CR0_PMAX, data);
224 /* update RST_CPL4, PCODE_INIT_DONE4 */
225 timedout = set_bios_reset_cpl_for_package(pcu1, RST_CPL4_MASK,
226 PCODE_INIT_DONE4_MASK, RST_CPL4_MASK);
227 if (timedout)
228 die("BIOS RESET CPL4 timed out.\n");
230 /* set CSR_DESIRED_CORES_CFG2 lock bit */
231 data = pci_read_config32(pcu1, PCU_CR1_DESIRED_CORES_CFG2_REG);
232 data |= PCU_CR1_DESIRED_CORES_CFG2_REG_LOCK_MASK;
233 printk(BIOS_SPEW, "%s - pci_write_config32 PCU_CR1_DESIRED_CORES_CFG2_REG 0x%x, data: 0x%x\n",
234 __func__, PCU_CR1_DESIRED_CORES_CFG2_REG, data);
235 pci_write_config32(pcu1, PCU_CR1_DESIRED_CORES_CFG2_REG, data);
238 void set_bios_init_completion(void)
240 uint32_t sbsp_socket_id = 0;
243 * According to the BIOS Writer's Guide, the SBSP must be the last socket
244 * to receive the BIOS init completion message. So, we send it to all non-SBSP
245 * sockets first.
247 for (uint32_t socket = 0; socket < CONFIG_MAX_SOCKET; ++socket) {
248 if (!soc_cpu_is_enabled(socket))
249 continue;
250 if (socket == sbsp_socket_id)
251 continue;
252 set_bios_init_completion_for_package(socket);
255 /* And finally, take care of the SBSP */
256 set_bios_init_completion_for_package(sbsp_socket_id);
258 #endif