blob: e0bf9bc5a438918dcd9a7557fef725daeb6af8a2 [file] [log] [blame]
Andrey Petrov662da6c2020-03-16 22:46:57 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Andrey Petrov662da6c2020-03-16 22:46:57 -07002
Marc Jones18960ce2020-11-02 12:41:12 -07003#include <assert.h>
4#include <commonlib/sort.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -07005#include <console/console.h>
Marc Jones5851f9d2020-11-02 15:30:10 -07006#include <delay.h>
Marc Jones18960ce2020-11-02 12:41:12 -07007#include <device/device.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -07008#include <device/pci.h>
Jonathan Zhang665d8702023-01-24 11:18:15 -08009#include <intelblocks/cfg.h>
Marc Jones18960ce2020-11-02 12:41:12 -070010#include <intelblocks/cpulib.h>
Jonathan Zhang665d8702023-01-24 11:18:15 -080011#include <intelpch/lockdown.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -070012#include <soc/pci_devs.h>
Marc Jones53b465d2020-10-15 15:16:45 -060013#include <soc/msr.h>
Marc Jones5851f9d2020-11-02 15:30:10 -070014#include <soc/soc_util.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -070015#include <soc/util.h>
Marc Jones5851f9d2020-11-02 15:30:10 -070016#include <timer.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -070017
Jonathan Zhang665d8702023-01-24 11:18:15 -080018void lock_pam0123(void)
19{
20 if (get_lockdown_config() != CHIPSET_LOCKDOWN_COREBOOT)
21 return;
22
23 /* section 16.3.19 of Intel doc. #612246 */
24 uint32_t pam0123_lock = 0x33333331;
25 uint32_t bus1 = get_socket_ubox_busno(0);
26
27 pci_s_write_config32(PCI_DEV(bus1, SAD_ALL_DEV, SAD_ALL_FUNC),
28 SAD_ALL_PAM0123_CSR, pam0123_lock);
29}
30
Andrey Petrov662da6c2020-03-16 22:46:57 -070031void unlock_pam_regions(void)
32{
Andrey Petrov662da6c2020-03-16 22:46:57 -070033 uint32_t pam0123_unlock_dram = 0x33333330;
34 uint32_t pam456_unlock_dram = 0x00333333;
Jonathan Zhangca520a72023-01-23 18:14:53 -080035 uint32_t bus1 = get_socket_ubox_busno(0);
Andrey Petrov662da6c2020-03-16 22:46:57 -070036
Andrey Petrov662da6c2020-03-16 22:46:57 -070037 pci_io_write_config32(PCI_DEV(bus1, SAD_ALL_DEV, SAD_ALL_FUNC),
38 SAD_ALL_PAM0123_CSR, pam0123_unlock_dram);
39 pci_io_write_config32(PCI_DEV(bus1, SAD_ALL_DEV, SAD_ALL_FUNC),
40 SAD_ALL_PAM456_CSR, pam456_unlock_dram);
41
42 uint32_t reg1 = pci_io_read_config32(PCI_DEV(bus1, SAD_ALL_DEV,
43 SAD_ALL_FUNC), SAD_ALL_PAM0123_CSR);
44 uint32_t reg2 = pci_io_read_config32(PCI_DEV(bus1, SAD_ALL_DEV,
45 SAD_ALL_FUNC), SAD_ALL_PAM456_CSR);
46 printk(BIOS_DEBUG, "%s:%s pam0123_csr: 0x%x, pam456_csr: 0x%x\n",
47 __FILE__, __func__, reg1, reg2);
48}
49
Marc Jones53b465d2020-10-15 15:16:45 -060050msr_t read_msr_ppin(void)
51{
52 msr_t ppin = {0};
53 msr_t msr;
54
55 /* If MSR_PLATFORM_INFO PPIN_CAP is 0, PPIN capability is not supported */
56 msr = rdmsr(MSR_PLATFORM_INFO);
57 if ((msr.lo & MSR_PPIN_CAP) == 0) {
58 printk(BIOS_ERR, "MSR_PPIN_CAP is 0, PPIN is not supported\n");
59 return ppin;
60 }
61
62 /* Access to MSR_PPIN is permitted only if MSR_PPIN_CTL LOCK is 0 and ENABLE is 1 */
63 msr = rdmsr(MSR_PPIN_CTL);
64 if (msr.lo & MSR_PPIN_CTL_LOCK) {
65 printk(BIOS_ERR, "MSR_PPIN_CTL_LOCK is 1, PPIN access is not allowed\n");
66 return ppin;
67 }
68
69 if ((msr.lo & MSR_PPIN_CTL_ENABLE) == 0) {
70 /* Set MSR_PPIN_CTL ENABLE to 1 */
71 msr.lo |= MSR_PPIN_CTL_ENABLE;
72 wrmsr(MSR_PPIN_CTL, msr);
73 }
74 ppin = rdmsr(MSR_PPIN);
Marc Jones53b465d2020-10-15 15:16:45 -060075 return ppin;
76}
Marc Jones18960ce2020-11-02 12:41:12 -070077
Angel Ponsd453da22021-11-03 16:10:56 +010078static unsigned int get_threads_per_package(void)
Marc Jones18960ce2020-11-02 12:41:12 -070079{
80 unsigned int core_count, thread_count;
81 cpu_read_topology(&core_count, &thread_count);
82 return thread_count;
83}
84
85int get_platform_thread_count(void)
86{
87 return soc_get_num_cpus() * get_threads_per_package();
88}
89
90const IIO_UDS *get_iio_uds(void)
91{
92 size_t hob_size;
Arthur Heymans12985c12020-11-06 11:45:41 +010093 static const IIO_UDS *hob;
Marc Jones18960ce2020-11-02 12:41:12 -070094 const uint8_t fsp_hob_iio_universal_data_guid[16] = FSP_HOB_IIO_UNIVERSAL_DATA_GUID;
95
Elyes Haouasf1ba7d62022-09-13 10:03:44 +020096 if (hob)
Arthur Heymans12985c12020-11-06 11:45:41 +010097 return hob;
98
Marc Jones18960ce2020-11-02 12:41:12 -070099 hob = fsp_find_extension_hob_by_guid(fsp_hob_iio_universal_data_guid, &hob_size);
Elyes Haouasf1ba7d62022-09-13 10:03:44 +0200100 assert(hob && hob_size != 0);
Marc Jones18960ce2020-11-02 12:41:12 -0700101 return hob;
102}
103
Arthur Heymans6408ada2020-11-12 17:33:00 +0100104void get_iiostack_info(struct iiostack_resource *info)
105{
106 const IIO_UDS *hob = get_iio_uds();
107
108 // copy IIO Stack info from FSP HOB
109 info->no_of_stacks = 0;
110 for (int s = 0; s < hob->PlatformData.numofIIO; ++s) {
111 for (int x = 0; x < MAX_IIO_STACK; ++x) {
112 const STACK_RES *ri = &hob->PlatformData.IIO_resource[s].StackRes[x];
113 if (!is_iio_stack_res(ri))
114 continue;
115 assert(info->no_of_stacks < (CONFIG_MAX_SOCKET * MAX_IIO_STACK));
116 memcpy(&info->res[info->no_of_stacks++], ri, sizeof(STACK_RES));
117 }
118 }
119}
120
Marc Jones18960ce2020-11-02 12:41:12 -0700121unsigned int soc_get_num_cpus(void)
122{
123 /* The FSP IIO UDS HOB has field numCpus, it is actually socket count */
124 return get_iio_uds()->SystemStatus.numCpus;
125}
126
127#if ENV_RAMSTAGE /* Setting devtree variables is only allowed in ramstage. */
Marc Jones5851f9d2020-11-02 15:30:10 -0700128/* return true if command timed out else false */
129static bool wait_for_bios_cmd_cpl(pci_devfn_t dev, uint32_t reg, uint32_t mask,
130 uint32_t target)
131{
132 const uint32_t max_delay = 5000; /* 5 seconds max */
133 const uint32_t step_delay = 50; /* 50 us */
134 struct stopwatch sw;
135
136 stopwatch_init_msecs_expire(&sw, max_delay);
137 while ((pci_s_read_config32(dev, reg) & mask) != target) {
138 udelay(step_delay);
139 if (stopwatch_expired(&sw)) {
140 printk(BIOS_ERR, "%s timed out for dev: %x, reg: 0x%x, "
141 "mask: 0x%x, target: 0x%x\n", __func__, dev, reg, mask, target);
142 return true; /* timedout */
143 }
144 }
145 return false; /* successful */
146}
147
148/* return true if command timed out else false */
149static bool write_bios_mailbox_cmd(pci_devfn_t dev, uint32_t command, uint32_t data)
150{
151 /* verify bios is not in busy state */
152 if (wait_for_bios_cmd_cpl(dev, PCU_CR1_BIOS_MB_INTERFACE_REG, BIOS_MB_RUN_BUSY_MASK, 0))
153 return true; /* timed out */
154
155 /* write data to data register */
156 printk(BIOS_SPEW, "%s - pci_s_write_config32 reg: 0x%x, data: 0x%x\n", __func__,
157 PCU_CR1_BIOS_MB_DATA_REG, data);
158 pci_s_write_config32(dev, PCU_CR1_BIOS_MB_DATA_REG, data);
159
160 /* write the command */
161 printk(BIOS_SPEW, "%s - pci_s_write_config32 reg: 0x%x, data: 0x%lx\n", __func__,
162 PCU_CR1_BIOS_MB_INTERFACE_REG, command | BIOS_MB_RUN_BUSY_MASK);
163 pci_s_write_config32(dev, PCU_CR1_BIOS_MB_INTERFACE_REG,
164 command | BIOS_MB_RUN_BUSY_MASK);
165
166 /* wait for completion or time out*/
167 return wait_for_bios_cmd_cpl(dev, PCU_CR1_BIOS_MB_INTERFACE_REG,
168 BIOS_MB_RUN_BUSY_MASK, 0);
169}
170
171/* return true if command timed out else false */
172static bool set_bios_reset_cpl_for_package(uint32_t socket, uint32_t rst_cpl_mask,
173 uint32_t pcode_init_mask, uint32_t val)
174{
Jonathan Zhangca520a72023-01-23 18:14:53 -0800175 const uint32_t bus = get_socket_ubox_busno(socket);
Marc Jones5851f9d2020-11-02 15:30:10 -0700176 const pci_devfn_t dev = PCI_DEV(bus, PCU_DEV, PCU_CR1_FUN);
177
178 uint32_t reg = pci_s_read_config32(dev, PCU_CR1_BIOS_RESET_CPL_REG);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100179 reg &= (uint32_t)~rst_cpl_mask;
Marc Jones5851f9d2020-11-02 15:30:10 -0700180 reg |= val;
181
182 /* update BIOS RESET completion bit */
183 pci_s_write_config32(dev, PCU_CR1_BIOS_RESET_CPL_REG, reg);
184
185 /* wait for PCU ack */
186 return wait_for_bios_cmd_cpl(dev, PCU_CR1_BIOS_RESET_CPL_REG, pcode_init_mask,
187 pcode_init_mask);
188}
189
190static void set_bios_init_completion_for_package(uint32_t socket)
191{
192 uint32_t data;
193 bool timedout;
Jonathan Zhangca520a72023-01-23 18:14:53 -0800194 const uint32_t bus = get_socket_ubox_busno(socket);
Marc Jones5851f9d2020-11-02 15:30:10 -0700195 const pci_devfn_t dev = PCI_DEV(bus, PCU_DEV, PCU_CR1_FUN);
196
197 /* read PCU config */
198 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_READ_PCU_MISC_CFG, 0);
199 if (timedout) {
200 /* 2nd try */
201 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_READ_PCU_MISC_CFG, 0);
202 if (timedout)
203 die("BIOS PCU Misc Config Read timed out.\n");
204
205 /* Since the 1st try failed, we need to make sure PCU is in stable state */
206 data = pci_s_read_config32(dev, PCU_CR1_BIOS_MB_DATA_REG);
207 printk(BIOS_SPEW, "%s - pci_s_read_config32 reg: 0x%x, data: 0x%x\n",
208 __func__, PCU_CR1_BIOS_MB_DATA_REG, data);
209 timedout = write_bios_mailbox_cmd(dev, BIOS_CMD_WRITE_PCU_MISC_CFG, data);
210 if (timedout)
211 die("BIOS PCU Misc Config Write timed out.\n");
212 }
213
214 /* update RST_CPL3, PCODE_INIT_DONE3 */
215 timedout = set_bios_reset_cpl_for_package(socket, RST_CPL3_MASK,
216 PCODE_INIT_DONE3_MASK, RST_CPL3_MASK);
217 if (timedout)
218 die("BIOS RESET CPL3 timed out.\n");
219
Marc Jones4fad28f2021-04-01 14:47:52 -0600220 /* Set PMAX_LOCK - must be set before RESET CPL4 */
Jonathan Zhangffc5a1c2023-01-23 16:33:03 -0800221 data = pci_s_read_config32(PCI_DEV(bus, PCU_DEV, PCU_CR0_FUN), PCU_CR0_PMAX);
222 data |= PMAX_LOCK;
223 pci_s_write_config32(PCI_DEV(bus, PCU_DEV, PCU_CR0_FUN), PCU_CR0_PMAX, data);
Marc Jones4fad28f2021-04-01 14:47:52 -0600224
Marc Jones5851f9d2020-11-02 15:30:10 -0700225 /* update RST_CPL4, PCODE_INIT_DONE4 */
226 timedout = set_bios_reset_cpl_for_package(socket, RST_CPL4_MASK,
227 PCODE_INIT_DONE4_MASK, RST_CPL4_MASK);
228 if (timedout)
229 die("BIOS RESET CPL4 timed out.\n");
230
231 /* set CSR_DESIRED_CORES_CFG2 lock bit */
232 data = pci_s_read_config32(dev, PCU_CR1_DESIRED_CORES_CFG2_REG);
233 data |= PCU_CR1_DESIRED_CORES_CFG2_REG_LOCK_MASK;
234 printk(BIOS_SPEW, "%s - pci_s_write_config32 PCU_CR1_DESIRED_CORES_CFG2_REG 0x%x, data: 0x%x\n",
235 __func__, PCU_CR1_DESIRED_CORES_CFG2_REG, data);
236 pci_s_write_config32(dev, PCU_CR1_DESIRED_CORES_CFG2_REG, data);
237}
238
239void set_bios_init_completion(void)
240{
241 /* FIXME: This may need to be changed for multi-socket platforms */
242 uint32_t sbsp_socket_id = 0;
243
244 /*
245 * According to the BIOS Writer's Guide, the SBSP must be the last socket
246 * to receive the BIOS init completion message. So, we send it to all non-SBSP
247 * sockets first.
248 */
249 for (uint32_t socket = 0; socket < soc_get_num_cpus(); ++socket) {
250 if (socket == sbsp_socket_id)
251 continue;
252 set_bios_init_completion_for_package(socket);
253 }
254
255 /* And finally, take care of the SBSP */
256 set_bios_init_completion_for_package(sbsp_socket_id);
257}
Marc Jones18960ce2020-11-02 12:41:12 -0700258#endif