Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <console/console.h> |
| 5 | #include <post.h> |
| 6 | #include <device/pci.h> |
| 7 | #include <soc/chip_common.h> |
| 8 | #include <soc/soc_util.h> |
| 9 | #include <soc/util.h> |
| 10 | #include <stdlib.h> |
| 11 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 12 | static const STACK_RES *domain_to_stack_res(const struct device *dev) |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 13 | { |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 14 | assert(dev->path.type == DEVICE_PATH_DOMAIN); |
Patrick Rudolph | 8c99ebc | 2024-01-19 17:28:47 +0100 | [diff] [blame] | 15 | const union xeon_domain_path dn = { |
| 16 | .domain_path = dev->path.domain.domain |
| 17 | }; |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 18 | |
| 19 | const IIO_UDS *hob = get_iio_uds(); |
| 20 | assert(hob != NULL); |
| 21 | |
Patrick Rudolph | 8c99ebc | 2024-01-19 17:28:47 +0100 | [diff] [blame] | 22 | return &hob->PlatformData.IIO_resource[dn.socket].StackRes[dn.stack]; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 23 | } |
| 24 | |
Patrick Rudolph | 9fa4048 | 2024-01-18 08:55:08 +0100 | [diff] [blame^] | 25 | /** |
| 26 | * Find a device of a given vendor and type for the specified socket. |
| 27 | * The function iterates over all PCI domains of the specified socket |
| 28 | * and matches the PCI vendor and device ID. |
| 29 | * |
| 30 | * @param socket The socket where to search for the device. |
| 31 | * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel). |
| 32 | * @param device A PCI device ID. |
| 33 | * @return Pointer to the device struct. |
| 34 | */ |
| 35 | struct device *dev_find_device_on_socket(uint8_t socket, u16 vendor, u16 device) |
| 36 | { |
| 37 | struct device *domain, *dev = NULL; |
| 38 | union xeon_domain_path dn; |
| 39 | |
| 40 | while ((dev = dev_find_device(vendor, device, dev))) { |
| 41 | domain = dev_get_pci_domain(dev); |
| 42 | if (!domain) |
| 43 | continue; |
| 44 | dn.domain_path = domain->path.domain.domain; |
| 45 | if (dn.socket != socket) |
| 46 | continue; |
| 47 | return dev; |
| 48 | } |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the socket ID where the specified device is connected to. |
| 55 | * This is an integer in the range [0, CONFIG_MAX_SOCKET). |
| 56 | * |
| 57 | * @param dev The device to look up |
| 58 | * |
| 59 | * @return Socket ID the device is attached to, negative number on error. |
| 60 | */ |
| 61 | int iio_pci_domain_socket_from_dev(struct device *dev) |
| 62 | { |
| 63 | struct device *domain; |
| 64 | union xeon_domain_path dn; |
| 65 | |
| 66 | if (dev->path.type == DEVICE_PATH_DOMAIN) |
| 67 | domain = dev; |
| 68 | else |
| 69 | domain = dev_get_pci_domain(dev); |
| 70 | |
| 71 | if (!domain) |
| 72 | return -1; |
| 73 | |
| 74 | dn.domain_path = domain->path.domain.domain; |
| 75 | |
| 76 | return dn.socket; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the stack ID where the specified device is connected to. |
| 81 | * This is an integer in the range [0, MAX_IIO_STACK). |
| 82 | * |
| 83 | * @param dev The device to look up |
| 84 | * |
| 85 | * @return Stack ID the device is attached to, negative number on error. |
| 86 | */ |
| 87 | int iio_pci_domain_stack_from_dev(struct device *dev) |
| 88 | { |
| 89 | struct device *domain; |
| 90 | union xeon_domain_path dn; |
| 91 | |
| 92 | if (dev->path.type == DEVICE_PATH_DOMAIN) |
| 93 | domain = dev; |
| 94 | else |
| 95 | domain = dev_get_pci_domain(dev); |
| 96 | |
| 97 | if (!domain) |
| 98 | return -1; |
| 99 | |
| 100 | dn.domain_path = domain->path.domain.domain; |
| 101 | |
| 102 | return dn.stack; |
| 103 | } |
| 104 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 105 | void iio_pci_domain_read_resources(struct device *dev) |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 106 | { |
| 107 | struct resource *res; |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 108 | const STACK_RES *sr = domain_to_stack_res(dev); |
| 109 | |
| 110 | if (!sr) |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 111 | return; |
| 112 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 113 | int index = 0; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 114 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 115 | if (dev->path.domain.domain == 0) { |
| 116 | /* The 0 - 0xfff IO range is not reported by the HOB but still gets decoded */ |
| 117 | res = new_resource(dev, index++); |
| 118 | res->base = 0; |
| 119 | res->size = 0x1000; |
| 120 | res->limit = 0xfff; |
| 121 | res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED; |
| 122 | } |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 123 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 124 | if (sr->PciResourceIoBase < sr->PciResourceIoLimit) { |
| 125 | res = new_resource(dev, index++); |
| 126 | res->base = sr->PciResourceIoBase; |
| 127 | res->limit = sr->PciResourceIoLimit; |
| 128 | res->size = res->limit - res->base + 1; |
| 129 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED; |
| 130 | } |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 131 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 132 | if (sr->PciResourceMem32Base < sr->PciResourceMem32Limit) { |
| 133 | res = new_resource(dev, index++); |
| 134 | res->base = sr->PciResourceMem32Base; |
| 135 | res->limit = sr->PciResourceMem32Limit; |
| 136 | res->size = res->limit - res->base + 1; |
| 137 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED; |
| 138 | } |
| 139 | |
| 140 | if (sr->PciResourceMem64Base < sr->PciResourceMem64Limit) { |
| 141 | res = new_resource(dev, index++); |
| 142 | res->base = sr->PciResourceMem64Base; |
| 143 | res->limit = sr->PciResourceMem64Limit; |
| 144 | res->size = res->limit - res->base + 1; |
| 145 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 149 | void iio_pci_domain_scan_bus(struct device *dev) |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 150 | { |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 151 | const STACK_RES *sr = domain_to_stack_res(dev); |
| 152 | if (!sr) |
| 153 | return; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 154 | |
Arthur Heymans | 3e99ba0 | 2024-01-25 22:26:07 +0100 | [diff] [blame] | 155 | struct bus *bus = alloc_bus(dev); |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 156 | bus->secondary = sr->BusBase; |
| 157 | bus->subordinate = sr->BusBase; |
| 158 | bus->max_subordinate = sr->BusLimit; |
| 159 | |
| 160 | printk(BIOS_SPEW, "Scanning IIO stack %d: busses %x-%x\n", dev->path.domain.domain, |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 161 | dev->downstream->secondary, dev->downstream->max_subordinate); |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 162 | pci_host_bridge_scan_bus(dev); |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Jonathan Zhang | a63ea89 | 2023-01-25 11:16:58 -0800 | [diff] [blame] | 165 | /* |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 166 | * Used by IIO stacks for PCIe bridges. Those contain 1 PCI host bridges, |
| 167 | * all the bus numbers on the IIO stack can be used for this bridge |
Jonathan Zhang | a63ea89 | 2023-01-25 11:16:58 -0800 | [diff] [blame] | 168 | */ |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 169 | static struct device_operations iio_pcie_domain_ops = { |
| 170 | .read_resources = iio_pci_domain_read_resources, |
| 171 | .set_resources = pci_domain_set_resources, |
| 172 | .scan_bus = iio_pci_domain_scan_bus, |
| 173 | }; |
Arthur Heymans | 165893b | 2020-11-06 12:15:41 +0100 | [diff] [blame] | 174 | |
Patrick Rudolph | 1567259 | 2024-01-18 07:57:07 +0100 | [diff] [blame] | 175 | /* |
| 176 | * Used by UBOX stacks. Those contain multiple PCI host bridges, each having |
| 177 | * only one bus with UBOX devices. UBOX devices have no resources. |
| 178 | */ |
| 179 | static struct device_operations ubox_pcie_domain_ops = { |
| 180 | .read_resources = noop_read_resources, |
| 181 | .set_resources = noop_set_resources, |
| 182 | .scan_bus = pci_host_bridge_scan_bus, |
| 183 | }; |
| 184 | |
| 185 | /* |
| 186 | * On the first Xeon-SP generations there are no separate UBOX stacks, |
| 187 | * and the UBOX devices reside on the first and second IIO. Starting |
| 188 | * with 3rd gen Xeon-SP the UBOX devices are located on their own IIO. |
| 189 | */ |
| 190 | static void soc_create_ubox_domains(const union xeon_domain_path dp, struct bus *upstream, |
| 191 | const unsigned int bus_base, const unsigned int bus_limit) |
| 192 | { |
| 193 | union xeon_domain_path new_path = { |
| 194 | .domain_path = dp.domain_path |
| 195 | }; |
| 196 | |
| 197 | for (int i = bus_base; i <= bus_limit; i++) { |
| 198 | new_path.bus = i; |
| 199 | |
| 200 | struct device_path path = { |
| 201 | .type = DEVICE_PATH_DOMAIN, |
| 202 | .domain = { |
| 203 | .domain = new_path.domain_path, |
| 204 | }, |
| 205 | }; |
| 206 | struct device *const domain = alloc_dev(upstream, &path); |
| 207 | if (!domain) |
| 208 | die("%s: out of memory.\n", __func__); |
| 209 | |
| 210 | domain->ops = &ubox_pcie_domain_ops; |
| 211 | |
| 212 | struct bus *const bus = alloc_bus(domain); |
| 213 | bus->secondary = i; |
| 214 | bus->subordinate = bus->secondary; |
| 215 | bus->max_subordinate = bus->secondary; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* Attach stack as domains */ |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 220 | void attach_iio_stacks(struct device *dev) |
| 221 | { |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 222 | const IIO_UDS *hob = get_iio_uds(); |
Patrick Rudolph | 8c99ebc | 2024-01-19 17:28:47 +0100 | [diff] [blame] | 223 | union xeon_domain_path dn = { .domain_path = 0 }; |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 224 | if (!hob) |
| 225 | return; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 226 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 227 | for (int s = 0; s < hob->PlatformData.numofIIO; ++s) { |
| 228 | for (int x = 0; x < MAX_LOGIC_IIO_STACK; ++x) { |
| 229 | if (s == 0 && x == 0) |
| 230 | continue; |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 231 | |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 232 | const STACK_RES *ri = &hob->PlatformData.IIO_resource[s].StackRes[x]; |
Arthur Heymans | 470f1d3 | 2023-08-31 18:19:09 +0200 | [diff] [blame] | 233 | if (ri->BusBase > ri->BusLimit) |
Arthur Heymans | 550f55e | 2022-08-24 14:44:26 +0200 | [diff] [blame] | 234 | continue; |
Jonathan Zhang | 532e8c0 | 2023-01-25 11:28:49 -0800 | [diff] [blame] | 235 | |
Patrick Rudolph | 8c99ebc | 2024-01-19 17:28:47 +0100 | [diff] [blame] | 236 | /* Prepare domain path */ |
| 237 | dn.socket = s; |
| 238 | dn.stack = x; |
| 239 | dn.bus = ri->BusBase; |
| 240 | |
Patrick Rudolph | 1567259 | 2024-01-18 07:57:07 +0100 | [diff] [blame] | 241 | if (is_ubox_stack_res(ri)) { |
| 242 | soc_create_ubox_domains(dn, dev->upstream, ri->BusBase, ri->BusLimit); |
| 243 | } else if (is_pcie_iio_stack_res(ri)) { |
| 244 | struct device_path path; |
| 245 | path.type = DEVICE_PATH_DOMAIN; |
| 246 | path.domain.domain = dn.domain_path; |
| 247 | struct device *iio_domain = alloc_dev(dev->upstream, &path); |
| 248 | if (iio_domain == NULL) |
| 249 | die("%s: out of memory.\n", __func__); |
Jonathan Zhang | 532e8c0 | 2023-01-25 11:28:49 -0800 | [diff] [blame] | 250 | |
Patrick Rudolph | 1567259 | 2024-01-18 07:57:07 +0100 | [diff] [blame] | 251 | iio_domain->ops = &iio_pcie_domain_ops; |
| 252 | } else if (CONFIG(HAVE_IOAT_DOMAINS)) |
| 253 | soc_create_ioat_domains(dn, dev->upstream, ri); |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 254 | } |
| 255 | } |
Marc Jones | 1f50084 | 2020-10-15 14:32:51 -0600 | [diff] [blame] | 256 | } |