blob: 69ca0637b17a33b4255fe294c5e26f4ddce33171 [file] [log] [blame]
Jonathan Zhang3ed903f2023-01-25 11:37:27 -08001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
Jonathan Zhang3ed903f2023-01-25 11:37:27 -08004#include <device/device.h>
5#include <device/pci_ops.h>
6#include <device/pci.h>
7#include <device/pciexp.h>
Shuo Liu5ed9fe92024-03-26 21:27:52 +08008#include <soc/chip_common.h>
Jonathan Zhang3ed903f2023-01-25 11:37:27 -08009#include <soc/numa.h>
10#include <soc/soc_util.h>
11#include <soc/util.h>
12#include <types.h>
13
14void dump_pds(void)
15{
16 printk(BIOS_DEBUG, "====== Proximity Domain Dump ======\n");
17 printk(BIOS_DEBUG, "number of proximity domains: %d\n", pds.num_pds);
18 for (uint8_t i = 0; i < pds.num_pds; i++) {
19 printk(BIOS_DEBUG, "\tproximity domain %d:\n", i);
20 printk(BIOS_DEBUG, "\t\ttype:%d\n", pds.pds[i].pd_type);
21 printk(BIOS_DEBUG, "\t\tsocket_bitmap:0x%x\n", pds.pds[i].socket_bitmap);
Patrick Rudolphf25d58c2024-01-31 11:31:55 +010022 printk(BIOS_DEBUG, "\t\tdevice:%s\n", pds.pds[i].dev ? dev_path(pds.pds[i].dev) : "");
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080023 printk(BIOS_DEBUG, "\t\tbase(64MB):0x%x\n", pds.pds[i].base);
24 printk(BIOS_DEBUG, "\t\tsize(64MB):0x%x\n", pds.pds[i].size);
25 }
26}
27
Patrick Rudolph8bbc07e2024-02-14 10:44:11 +010028void fill_pds(void)
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080029{
30 uint8_t num_sockets = soc_get_num_cpus();
31 uint8_t num_cxlnodes = get_cxl_node_count();
Patrick Rudolph92904bc2023-07-14 17:50:52 +020032 const IIO_UDS *hob = get_iio_uds();
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080033
34 /*
35 * Rules/assumptions:
36 * 1. Each processor has a processor proximity domain regardless whether
37 * a processor has DIMM attached to it or not.
38 * 2. All system memory map elements are either from processor attached memory,
39 * or from CXL memory. Each CXL node info entry has a corresponding entry
40 * in system memory map elements.
41 * 3. Each CXL device may have multiple HDMs (Host-managed Device Memory). Each
42 * HDM has one and only one CXL node info entry. Each CXL node info entry
43 * represents a generic initiator proximity domain.
44 */
45 pds.num_pds = num_cxlnodes + num_sockets;
46 pds.pds = xmalloc(sizeof(struct proximity_domain) * pds.num_pds);
47 if (!pds.pds)
Nico Huberfeba51b2023-05-15 18:33:50 +020048 die("%s %d out of memory.", __FILE__, __LINE__);
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080049
50 memset(pds.pds, 0, sizeof(struct proximity_domain) * pds.num_pds);
51
52 /* Fill in processor domains */
Shuo Liu9580e7f2024-03-27 04:26:16 +080053 uint8_t i = 0;
54 for (uint8_t socket = 0; socket < num_sockets; socket++) {
Patrick Rudolphac028572023-07-14 17:44:33 +020055 if (!soc_cpu_is_enabled(socket))
56 continue;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080057 pds.pds[i].pd_type = PD_TYPE_PROCESSOR;
Patrick Rudolphac028572023-07-14 17:44:33 +020058 pds.pds[i].socket_bitmap = 1 << hob->PlatformData.IIO_resource[socket].SocketID;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080059 pds.pds[i].distances = malloc(sizeof(uint8_t) * pds.num_pds);
60 if (!pds.pds[i].distances)
Nico Huberfeba51b2023-05-15 18:33:50 +020061 die("%s %d out of memory.", __FILE__, __LINE__);
Patrick Rudolphac028572023-07-14 17:44:33 +020062 i++;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080063 }
64
65 /* If there are no CXL nodes, we are done */
66 if (num_cxlnodes == 0)
Patrick Rudolph8bbc07e2024-02-14 10:44:11 +010067 return;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080068
Shuo Liu1c39bcc2024-03-26 22:57:18 +080069#if CONFIG(SOC_INTEL_HAS_CXL)
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080070 /* There are CXL nodes, fill in generic initiator domain after the processors pds */
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080071 const CXL_NODE_SOCKET *cxl_hob = get_cxl_node();
Shuo Liu9580e7f2024-03-27 04:26:16 +080072 for (uint8_t skt_id = 0; skt_id < MAX_SOCKET; skt_id++) {
73 for (uint8_t cxl_id = 0; cxl_id < cxl_hob[skt_id].CxlNodeCount; ++cxl_id) {
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080074 const CXL_NODE_INFO node = cxl_hob[skt_id].CxlNodeInfo[cxl_id];
75 pds.pds[i].pd_type = PD_TYPE_GENERIC_INITIATOR;
76 pds.pds[i].socket_bitmap = node.SocketBitmap;
77 pds.pds[i].base = node.Address;
78 pds.pds[i].size = node.Size;
Shuo Liu1c39bcc2024-03-26 22:57:18 +080079 struct device *dev = pcie_find_dsn(node.SerialNumber, node.VendorId, 0);
Patrick Rudolphf25d58c2024-01-31 11:31:55 +010080 pds.pds[i].dev = dev;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080081 pds.pds[i].distances = malloc(sizeof(uint8_t) * pds.num_pds);
82 if (!pds.pds[i].distances)
Nico Huberfeba51b2023-05-15 18:33:50 +020083 die("%s %d out of memory.", __FILE__, __LINE__);
Shuo Liu9580e7f2024-03-27 04:26:16 +080084 i++;
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080085 }
86 }
Shuo Liu1c39bcc2024-03-26 22:57:18 +080087#endif
Jonathan Zhang3ed903f2023-01-25 11:37:27 -080088}
89
90/*
91 * Return the total size of memory regions in generic initiator affinity domains.
92 * The size is in unit of 64MB.
93 */
94uint32_t get_generic_initiator_mem_size(void)
95{
96 uint8_t i;
97 uint32_t size = 0;
98
99 for (i = 0; i < pds.num_pds; i++) {
100 if (pds.pds[i].pd_type == PD_TYPE_PROCESSOR)
101 continue;
102 size += pds.pds[i].size;
103 }
104
105 return size;
106}
Shuo Liu5ed9fe92024-03-26 21:27:52 +0800107
108static uint32_t socket_to_pd(uint8_t socket)
109{
110 for (uint8_t i = 0; i < pds.num_pds; i++) {
111 if (pds.pds[i].pd_type != PD_TYPE_PROCESSOR)
112 continue;
113 if (pds.pds[i].socket_bitmap == (1 << socket))
114 return i;
115 }
116
117 printk(BIOS_ERR, "%s: could not find proximity domain for socket %d.\n",
118 __func__, socket);
119
120 return XEONSP_INVALID_PD_INDEX;
121}
122
123uint32_t device_to_pd(const struct device *dev)
124{
125 /* first to see if the dev is bound to specific pd */
126 for (int i = 0; i < pds.num_pds; i++)
127 if (pds.pds[i].dev == dev)
128 return i;
129
130 if (dev->path.type == DEVICE_PATH_APIC)
131 return socket_to_pd(dev->path.apic.package_id);
132
133 if ((dev->path.type == DEVICE_PATH_DOMAIN) ||
134 (dev->path.type == DEVICE_PATH_PCI))
135 return socket_to_pd(iio_pci_domain_socket_from_dev(dev));
136
137 printk(BIOS_ERR, "%s: could not find proximity domain for device %s.\n",
138 __func__, dev_path(dev));
139
140 return XEONSP_INVALID_PD_INDEX;
141}
142
143uint32_t memory_to_pd(const struct SystemMemoryMapElement *mem)
144{
145 return socket_to_pd(mem->SocketId);
146}
Shuo Liu9580e7f2024-03-27 04:26:16 +0800147
148#define PD_DISTANCE_SELF 0x0A
149#define PD_DISTANCE_SAME_SOCKET 0x0C
150#define PD_DISTANCE_CROSS_SOCKET 0x14
151#define PD_DISTANCE_MAX 0xFF
152#define PD_DISTANCE_IO_EXTRA 0x01
153
154void fill_pd_distances(void)
155{
156 for (int i = 0; i < pds.num_pds; i++) {
157 for (int j = 0; j < pds.num_pds; j++) {
158 if (i == j) {
159 pds.pds[i].distances[j] = PD_DISTANCE_SELF;
160 continue;
161 }
162
163 if (pds.pds[i].socket_bitmap == pds.pds[j].socket_bitmap)
164 pds.pds[i].distances[j] = PD_DISTANCE_SAME_SOCKET;
165 else
166 pds.pds[i].distances[j] = PD_DISTANCE_CROSS_SOCKET;
167
168 if (pds.pds[i].pd_type == PD_TYPE_GENERIC_INITIATOR)
169 pds.pds[i].distances[j] += PD_DISTANCE_IO_EXTRA;
170
171 if (pds.pds[j].pd_type == PD_TYPE_GENERIC_INITIATOR)
172 pds.pds[i].distances[j] += PD_DISTANCE_IO_EXTRA;
173 }
174 }
175}