Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <console/console.h> |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 4 | #include <device/device.h> |
| 5 | #include <device/pci_ops.h> |
| 6 | #include <device/pci.h> |
| 7 | #include <device/pciexp.h> |
Shuo Liu | 5ed9fe9 | 2024-03-26 21:27:52 +0800 | [diff] [blame] | 8 | #include <soc/chip_common.h> |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 9 | #include <soc/numa.h> |
| 10 | #include <soc/soc_util.h> |
| 11 | #include <soc/util.h> |
| 12 | #include <types.h> |
| 13 | |
| 14 | void 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 Rudolph | f25d58c | 2024-01-31 11:31:55 +0100 | [diff] [blame] | 22 | printk(BIOS_DEBUG, "\t\tdevice:%s\n", pds.pds[i].dev ? dev_path(pds.pds[i].dev) : ""); |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 23 | 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 Rudolph | 8bbc07e | 2024-02-14 10:44:11 +0100 | [diff] [blame] | 28 | void fill_pds(void) |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 29 | { |
| 30 | uint8_t num_sockets = soc_get_num_cpus(); |
| 31 | uint8_t num_cxlnodes = get_cxl_node_count(); |
Patrick Rudolph | 92904bc | 2023-07-14 17:50:52 +0200 | [diff] [blame] | 32 | const IIO_UDS *hob = get_iio_uds(); |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 33 | |
| 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 Huber | feba51b | 2023-05-15 18:33:50 +0200 | [diff] [blame] | 48 | die("%s %d out of memory.", __FILE__, __LINE__); |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 49 | |
| 50 | memset(pds.pds, 0, sizeof(struct proximity_domain) * pds.num_pds); |
| 51 | |
| 52 | /* Fill in processor domains */ |
Shuo Liu | 9580e7f | 2024-03-27 04:26:16 +0800 | [diff] [blame^] | 53 | uint8_t i = 0; |
| 54 | for (uint8_t socket = 0; socket < num_sockets; socket++) { |
Patrick Rudolph | ac02857 | 2023-07-14 17:44:33 +0200 | [diff] [blame] | 55 | if (!soc_cpu_is_enabled(socket)) |
| 56 | continue; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 57 | pds.pds[i].pd_type = PD_TYPE_PROCESSOR; |
Patrick Rudolph | ac02857 | 2023-07-14 17:44:33 +0200 | [diff] [blame] | 58 | pds.pds[i].socket_bitmap = 1 << hob->PlatformData.IIO_resource[socket].SocketID; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 59 | pds.pds[i].distances = malloc(sizeof(uint8_t) * pds.num_pds); |
| 60 | if (!pds.pds[i].distances) |
Nico Huber | feba51b | 2023-05-15 18:33:50 +0200 | [diff] [blame] | 61 | die("%s %d out of memory.", __FILE__, __LINE__); |
Patrick Rudolph | ac02857 | 2023-07-14 17:44:33 +0200 | [diff] [blame] | 62 | i++; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* If there are no CXL nodes, we are done */ |
| 66 | if (num_cxlnodes == 0) |
Patrick Rudolph | 8bbc07e | 2024-02-14 10:44:11 +0100 | [diff] [blame] | 67 | return; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 68 | |
Shuo Liu | 1c39bcc | 2024-03-26 22:57:18 +0800 | [diff] [blame] | 69 | #if CONFIG(SOC_INTEL_HAS_CXL) |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 70 | /* There are CXL nodes, fill in generic initiator domain after the processors pds */ |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 71 | const CXL_NODE_SOCKET *cxl_hob = get_cxl_node(); |
Shuo Liu | 9580e7f | 2024-03-27 04:26:16 +0800 | [diff] [blame^] | 72 | 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 Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 74 | 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 Liu | 1c39bcc | 2024-03-26 22:57:18 +0800 | [diff] [blame] | 79 | struct device *dev = pcie_find_dsn(node.SerialNumber, node.VendorId, 0); |
Patrick Rudolph | f25d58c | 2024-01-31 11:31:55 +0100 | [diff] [blame] | 80 | pds.pds[i].dev = dev; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 81 | pds.pds[i].distances = malloc(sizeof(uint8_t) * pds.num_pds); |
| 82 | if (!pds.pds[i].distances) |
Nico Huber | feba51b | 2023-05-15 18:33:50 +0200 | [diff] [blame] | 83 | die("%s %d out of memory.", __FILE__, __LINE__); |
Shuo Liu | 9580e7f | 2024-03-27 04:26:16 +0800 | [diff] [blame^] | 84 | i++; |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
Shuo Liu | 1c39bcc | 2024-03-26 22:57:18 +0800 | [diff] [blame] | 87 | #endif |
Jonathan Zhang | 3ed903f | 2023-01-25 11:37:27 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Return the total size of memory regions in generic initiator affinity domains. |
| 92 | * The size is in unit of 64MB. |
| 93 | */ |
| 94 | uint32_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 Liu | 5ed9fe9 | 2024-03-26 21:27:52 +0800 | [diff] [blame] | 107 | |
| 108 | static 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 | |
| 123 | uint32_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 | |
| 143 | uint32_t memory_to_pd(const struct SystemMemoryMapElement *mem) |
| 144 | { |
| 145 | return socket_to_pd(mem->SocketId); |
| 146 | } |
Shuo Liu | 9580e7f | 2024-03-27 04:26:16 +0800 | [diff] [blame^] | 147 | |
| 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 | |
| 154 | void 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 | } |