mb/starlabs/{lite_adl,byte_adl}: Don't select MAINBOARD_HAS_TPM2
[coreboot2.git] / src / soc / intel / xeon_sp / chip_common.c
blob011e1c3297fcc2d4fd4377196fe88ed4492f645c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <acpi/acpigen_pci.h>
4 #include <assert.h>
5 #include <console/console.h>
6 #include <device/pci.h>
7 #include <intelblocks/acpi.h>
8 #include <soc/acpi.h>
9 #include <soc/chip_common.h>
10 #include <soc/soc_util.h>
11 #include <soc/util.h>
13 /**
14 * Find all device of a given vendor and type for the specified socket.
15 * The function iterates over all PCI domains of the specified socket
16 * and matches the PCI vendor and device ID.
18 * @param socket The socket where to search for the device.
19 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
20 * @param device A PCI device ID.
21 * @param from The device pointer to start search from.
23 * @return Pointer to the device struct. When there are multiple device
24 * instances, the caller should continue search upon a non-NULL match.
26 struct device *dev_find_all_devices_on_socket(uint8_t socket, u16 vendor, u16 device,
27 struct device *from)
29 return dev_find_all_devices_on_stack(socket, XEONSP_STACK_MAX, vendor, device, from);
33 * Find device of a given vendor and type for the specified socket.
34 * The function will return at the 1st match.
36 struct device *dev_find_device_on_socket(uint8_t socket, u16 vendor, u16 device)
38 return dev_find_all_devices_on_socket(socket, vendor, device, NULL);
41 static int filter_device_on_stack(struct device *dev, uint8_t socket, uint8_t stack,
42 u16 vendor, u16 device)
44 if (dev->path.type != DEVICE_PATH_PCI)
45 return 0;
47 const struct device *domain = dev_get_domain(dev);
48 if (!domain)
49 return 0;
51 union xeon_domain_path dn;
52 dn.domain_path = dev_get_domain_id(domain);
54 if (socket != XEONSP_SOCKET_MAX && dn.socket != socket)
55 return 0;
56 if (stack != XEONSP_STACK_MAX && dn.stack != stack)
57 return 0;
58 if (vendor != XEONSP_VENDOR_MAX && dev->vendor != vendor)
59 return 0;
60 if (device != XEONSP_DEVICE_MAX && dev->device != device)
61 return 0;
63 return 1;
66 /**
67 * Find all device of a given vendor and type for the specified socket and stack.
69 * @param socket The socket where to search for the device.
70 * XEONSP_SOCKET_MAX indicates any socket.
71 * @param stack The stack where to search for the device.
72 * XEONSP_STACK_MAX indicates any stack.
73 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
74 * XEONSP_VENDOR_MAX indicates any vendor.
75 * @param device A PCI device ID.
76 * XEONSP_DEVICE_MAX indicates any device.
77 * @param from The device pointer to start search from.
79 * @return Pointer to the device struct. When there are multiple device
80 * instances, the caller should continue search upon a non-NULL match.
82 struct device *dev_find_all_devices_on_stack(uint8_t socket, uint8_t stack,
83 u16 vendor, u16 device, struct device *from)
85 if (!from)
86 from = all_devices;
87 else
88 from = from->next;
90 while (from && (!filter_device_on_stack(from, socket, stack,
91 vendor, device)))
92 from = from->next;
94 return from;
97 /**
98 * Find all device of a given vendor and type for the specific domain
99 * Only the direct child of the input domain is iterated
101 * @param domain Pointer to the input domain
102 * @param vendor A PCI vendor ID
103 * XEONSP_VENDOR_MAX indicates any vendor
104 * @param vendor A PCI device ID
105 * XEONSP_DEVICE_MAX indicates any vendor
106 * @param from The device pointer to start search from.
108 * @return Pointer to the device struct. When there are multiple device
109 * instances, the caller should continue search upon a non-NULL match.
111 struct device *dev_find_all_devices_on_domain(struct device *domain, u16 vendor,
112 u16 device, struct device *from)
114 struct device *dev = from;
115 while ((dev = dev_bus_each_child(domain->downstream, dev))) {
116 if (vendor != XEONSP_VENDOR_MAX && dev->vendor != vendor)
117 continue;
118 if (device != XEONSP_DEVICE_MAX && dev->device != device)
119 continue;
120 break;
123 return dev;
127 * Returns the socket ID where the specified device is connected to.
128 * This is an integer in the range [0, CONFIG_MAX_SOCKET).
130 * @param dev The device to look up
132 * @return Socket ID the device is attached to, negative number on error.
134 int iio_pci_domain_socket_from_dev(const struct device *dev)
136 const struct device *domain;
137 union xeon_domain_path dn;
139 domain = dev_get_domain(dev);
140 if (!domain)
141 return -1;
143 dn.domain_path = dev_get_domain_id(domain);
145 return dn.socket;
149 * Returns the stack ID where the specified device is connected to.
150 * This is an integer in the range [0, MAX_IIO_STACK).
152 * @param dev The device to look up
154 * @return Stack ID the device is attached to, negative number on error.
156 int iio_pci_domain_stack_from_dev(const struct device *dev)
158 const struct device *domain;
159 union xeon_domain_path dn;
161 domain = dev_get_domain(dev);
162 if (!domain)
163 return -1;
165 dn.domain_path = dev_get_domain_id(domain);
167 return dn.stack;
170 void create_domain(const union xeon_domain_path dp, struct bus *upstream,
171 int bus_base, int bus_limit, const char *type,
172 struct device_operations *ops,
173 const size_t pci_segment_group)
175 struct device_path path;
176 init_xeon_domain_path(&path, dp.socket, dp.stack, bus_base);
178 struct device *const domain = alloc_find_dev(upstream, &path);
179 if (!domain)
180 die("%s: out of memory.\n", __func__);
182 domain->ops = ops;
183 iio_domain_set_acpi_name(domain, type);
185 struct bus *const bus = alloc_bus(domain);
186 bus->secondary = bus_base;
187 bus->subordinate = bus_base;
188 bus->max_subordinate = bus_limit;
189 bus->segment_group = pci_segment_group;
192 /* Attach stack as domains */
193 void attach_iio_stacks(void)
195 const IIO_UDS *hob = get_iio_uds();
196 union xeon_domain_path dn = { .domain_path = 0 };
197 if (!hob)
198 return;
200 struct bus *root_bus = dev_root.downstream;
201 for (int s = 0; s < CONFIG_MAX_SOCKET; ++s) {
202 if (!soc_cpu_is_enabled(s))
203 continue;
204 for (int x = 0; x < MAX_LOGIC_IIO_STACK; ++x) {
205 const xSTACK_RES *ri = &hob->PlatformData.IIO_resource[s].StackRes[x];
206 const size_t seg = hob->PlatformData.CpuQpiInfo[s].PcieSegment;
208 if (ri->BusBase > ri->BusLimit)
209 continue;
211 /* Prepare domain path */
212 dn.socket = s;
213 dn.stack = x;
215 create_xeonsp_domains(dn, root_bus, ri, seg);
220 bool is_pcie_domain(const struct device *dev)
222 if ((!dev) || (dev->path.type != DEVICE_PATH_DOMAIN))
223 return false;
225 return strstr(dev->name, DOMAIN_TYPE_PCIE);
228 bool is_ioat_domain(const struct device *dev)
230 if ((!dev) || (dev->path.type != DEVICE_PATH_DOMAIN))
231 return false;
233 return (strstr(dev->name, DOMAIN_TYPE_CPM0) ||
234 strstr(dev->name, DOMAIN_TYPE_CPM1) ||
235 strstr(dev->name, DOMAIN_TYPE_DINO) ||
236 strstr(dev->name, DOMAIN_TYPE_HQM0) ||
237 strstr(dev->name, DOMAIN_TYPE_HQM1));
240 bool is_ubox_domain(const struct device *dev)
242 if ((!dev) || (dev->path.type != DEVICE_PATH_DOMAIN))
243 return false;
245 return (strstr(dev->name, DOMAIN_TYPE_UBX0) ||
246 strstr(dev->name, DOMAIN_TYPE_UBX1));
249 bool is_cxl_domain(const struct device *dev)
251 if ((!dev) || (dev->path.type != DEVICE_PATH_DOMAIN))
252 return false;
254 return strstr(dev->name, DOMAIN_TYPE_CXL);