AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Soby Mathew | 5929bfe | 2024-11-28 12:28:00 +0000 | [diff] [blame] | 8 | #include <errno.h> |
AlexeiFedorov | 9f2de63 | 2024-09-10 11:48:22 +0100 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 11 | #include <debug.h> |
| 12 | #include <mmio.h> |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 13 | #include <pcie.h> |
Soby Mathew | 5929bfe | 2024-11-28 12:28:00 +0000 | [diff] [blame] | 14 | #include <pcie_doe.h> |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 15 | #include <pcie_spec.h> |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 16 | #include <platform.h> |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 17 | #include <tftf_lib.h> |
| 18 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 19 | #define PCIE_DEBUG VERBOSE |
| 20 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 21 | const struct pcie_info_table *g_pcie_info_table; |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 22 | pcie_device_bdf_table_t *g_pcie_bdf_table; |
| 23 | |
| 24 | pcie_device_bdf_table_t pcie_bdf_table[PCIE_DEVICE_BDF_TABLE_SZ]; |
| 25 | |
| 26 | uintptr_t pcie_cfg_addr(uint32_t bdf) |
| 27 | { |
| 28 | uint32_t bus = PCIE_EXTRACT_BDF_BUS(bdf); |
| 29 | uint32_t dev = PCIE_EXTRACT_BDF_DEV(bdf); |
| 30 | uint32_t func = PCIE_EXTRACT_BDF_FUNC(bdf); |
| 31 | uint32_t segment = PCIE_EXTRACT_BDF_SEG(bdf); |
| 32 | uint32_t cfg_addr; |
| 33 | uintptr_t ecam_base = 0; |
| 34 | unsigned int i = 0; |
| 35 | |
| 36 | assert((bus < PCIE_MAX_BUS) && (dev < PCIE_MAX_DEV) && (func < PCIE_MAX_FUNC)); |
| 37 | assert(g_pcie_info_table != NULL); |
| 38 | |
| 39 | while (i < g_pcie_info_table->num_entries) { |
| 40 | /* Derive ECAM specific information */ |
| 41 | const pcie_info_block_t *block = &g_pcie_info_table->block[i]; |
| 42 | |
| 43 | if ((bus >= block->start_bus_num) && |
| 44 | (bus <= block->end_bus_num) && |
| 45 | (segment == block->segment_num)) { |
| 46 | ecam_base = block->ecam_base; |
| 47 | break; |
| 48 | } |
| 49 | i++; |
| 50 | } |
| 51 | |
| 52 | assert(ecam_base != 0); |
| 53 | |
| 54 | /* |
| 55 | * There are 8 functions / device |
| 56 | * 32 devices / Bus and each has a 4KB config space |
| 57 | */ |
| 58 | cfg_addr = (bus * PCIE_MAX_DEV * PCIE_MAX_FUNC * PCIE_CFG_SIZE) + |
| 59 | (dev * PCIE_MAX_FUNC * PCIE_CFG_SIZE) + (func * PCIE_CFG_SIZE); |
| 60 | |
| 61 | return ecam_base + cfg_addr; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * @brief This API reads 32-bit data from PCIe config space pointed by Bus, |
| 66 | * Device, Function and register offset. |
| 67 | * 1. Caller - Test Suite |
| 68 | * 2. Prerequisite - pcie_create_info_table |
| 69 | * @param bdf - concatenated Bus(8-bits), device(8-bits) & function(8-bits) |
| 70 | * @param offset - Register offset within a device PCIe config space |
| 71 | * |
| 72 | * @return 32-bit data read from the config space |
| 73 | */ |
| 74 | uint32_t pcie_read_cfg(uint32_t bdf, uint32_t offset) |
| 75 | { |
| 76 | uintptr_t addr = pcie_cfg_addr(bdf); |
| 77 | |
| 78 | return mmio_read_32(addr + offset); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * @brief This API writes 32-bit data to PCIe config space pointed by Bus, |
| 83 | * Device, Function and register offset. |
| 84 | * 1. Caller - Test Suite |
| 85 | * 2. Prerequisite - val_pcie_create_info_table |
| 86 | * @param bdf - concatenated Bus(8-bits), device(8-bits) & function(8-bits) |
| 87 | * @param offset - Register offset within a device PCIe config space |
| 88 | * @param data - data to be written to the config space |
| 89 | * |
| 90 | * @return None |
| 91 | */ |
| 92 | void pcie_write_cfg(uint32_t bdf, uint32_t offset, uint32_t data) |
| 93 | { |
| 94 | uintptr_t addr = pcie_cfg_addr(bdf); |
| 95 | |
| 96 | mmio_write_32(addr + offset, data); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * @brief Check if BDF is PCIe Host Bridge. |
| 101 | * |
| 102 | * @param bdf - Function's Segment/Bus/Dev/Func in PCIE_CREATE_BDF format |
| 103 | * @return false If not a Host Bridge, true If it's a Host Bridge. |
| 104 | */ |
| 105 | bool pcie_is_host_bridge(uint32_t bdf) |
| 106 | { |
| 107 | uint32_t reg_value = pcie_read_cfg(bdf, TYPE01_RIDR); |
| 108 | |
| 109 | if ((HB_BASE_CLASS == ((reg_value >> CC_BASE_SHIFT) & CC_BASE_MASK)) && |
| 110 | (HB_SUB_CLASS == ((reg_value >> CC_SUB_SHIFT) & CC_SUB_MASK))) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * @brief Find a Function's config capability offset matching it's input parameter |
| 119 | * cid. cid_offset set to the matching cpability offset w.r.t. zero. |
| 120 | * |
| 121 | * @param bdf - Segment/Bus/Dev/Func in the format of PCIE_CREATE_BDF |
| 122 | * @param cid - Capability ID |
| 123 | * @param cid_offset - On return, points to cid offset in Function config space |
| 124 | * @return PCIE_CAP_NOT_FOUND, if there was a failure in finding required capability. |
| 125 | * PCIE_SUCCESS, if the search was successful. |
| 126 | */ |
| 127 | uint32_t pcie_find_capability(uint32_t bdf, uint32_t cid_type, uint32_t cid, |
| 128 | uint32_t *cid_offset) |
| 129 | { |
| 130 | uint32_t reg_value, next_cap_offset; |
| 131 | |
| 132 | if (cid_type == PCIE_CAP) { |
| 133 | /* Search in PCIe configuration space */ |
| 134 | reg_value = pcie_read_cfg(bdf, TYPE01_CPR); |
| 135 | |
| 136 | next_cap_offset = (reg_value & TYPE01_CPR_MASK); |
| 137 | while (next_cap_offset != 0) { |
| 138 | reg_value = pcie_read_cfg(bdf, next_cap_offset); |
| 139 | if ((reg_value & PCIE_CIDR_MASK) == cid) { |
| 140 | *cid_offset = next_cap_offset; |
| 141 | return PCIE_SUCCESS; |
| 142 | } |
| 143 | next_cap_offset = ((reg_value >> PCIE_NCPR_SHIFT) & |
| 144 | PCIE_NCPR_MASK); |
| 145 | } |
| 146 | } else if (cid_type == PCIE_ECAP) { |
| 147 | /* Search in PCIe extended configuration space */ |
| 148 | next_cap_offset = PCIE_ECAP_START; |
| 149 | while (next_cap_offset != 0) { |
| 150 | reg_value = pcie_read_cfg(bdf, next_cap_offset); |
| 151 | if ((reg_value & PCIE_ECAP_CIDR_MASK) == cid) { |
| 152 | *cid_offset = next_cap_offset; |
| 153 | return PCIE_SUCCESS; |
| 154 | } |
| 155 | next_cap_offset = ((reg_value >> PCIE_ECAP_NCPR_SHIFT) & |
| 156 | PCIE_ECAP_NCPR_MASK); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* The capability was not found */ |
| 161 | return PCIE_CAP_NOT_FOUND; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * @brief This API is used as placeholder to check if the bdf |
| 166 | * obtained is valid or not |
| 167 | * |
| 168 | * @param bdf |
| 169 | * @return true if bdf is valid else false |
| 170 | */ |
| 171 | bool pcie_check_device_valid(uint32_t bdf) |
| 172 | { |
| 173 | (void) bdf; |
| 174 | /* |
| 175 | * Add BDFs to this function if PCIe tests |
| 176 | * need to be ignored for a BDF for any reason |
| 177 | */ |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * @brief Returns whether a PCIe Function is an on-chip peripheral or not |
| 183 | * |
| 184 | * @param bdf - Segment/Bus/Dev/Func in the format of PCIE_CREATE_BDF |
| 185 | * @return Returns TRUE if the Function is on-chip peripheral, FALSE if it is |
| 186 | * not an on-chip peripheral |
| 187 | */ |
| 188 | bool pcie_is_onchip_peripheral(uint32_t bdf) |
| 189 | { |
| 190 | (void)bdf; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * @brief Returns the type of pcie device or port for the given bdf |
| 196 | * |
| 197 | * @param bdf - Segment/Bus/Dev/Func in the format of PCIE_CREATE_BDF |
| 198 | * @return Returns (1 << 0b1001) for RCiEP, (1 << 0b1010) for RCEC, |
| 199 | * (1 << 0b0000) for EP, (1 << 0b0100) for RP, |
| 200 | * (1 << 0b1100) for iEP_EP, (1 << 0b1011) for iEP_RP, |
| 201 | * (1 << PCIECR[7:4]) for any other device type. |
| 202 | */ |
| 203 | uint32_t pcie_device_port_type(uint32_t bdf) |
| 204 | { |
| 205 | uint32_t pciecs_base, reg_value, dp_type; |
| 206 | |
| 207 | /* |
| 208 | * Get the PCI Express Capability structure offset and |
| 209 | * use that offset to read pci express capabilities register |
| 210 | */ |
| 211 | pcie_find_capability(bdf, PCIE_CAP, CID_PCIECS, &pciecs_base); |
| 212 | reg_value = pcie_read_cfg(bdf, pciecs_base + CIDR_OFFSET); |
| 213 | |
| 214 | /* Read Device/Port bits [7:4] in Function's PCIe Capabilities register */ |
| 215 | dp_type = (reg_value >> ((PCIECR_OFFSET - CIDR_OFFSET)*8 + |
| 216 | PCIECR_DPT_SHIFT)) & PCIECR_DPT_MASK; |
| 217 | dp_type = (1 << dp_type); |
| 218 | |
| 219 | /* Check if the device/port is an on-chip peripheral */ |
| 220 | if (pcie_is_onchip_peripheral(bdf)) { |
| 221 | if (dp_type == EP) { |
| 222 | dp_type = iEP_EP; |
| 223 | } else if (dp_type == RP) { |
| 224 | dp_type = iEP_RP; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* Return device/port type */ |
| 229 | return dp_type; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * @brief Returns BDF of the upstream Root Port of a pcie device function. |
| 234 | * |
| 235 | * @param bdf - Function's Segment/Bus/Dev/Func in PCIE_CREATE_BDF format |
| 236 | * @param usrp_bdf - Upstream Rootport bdf in PCIE_CREATE_BDF format |
| 237 | * @return 0 for success, 1 for failure. |
| 238 | */ |
| 239 | uint32_t pcie_get_rootport(uint32_t bdf, uint32_t *rp_bdf) |
| 240 | { |
| 241 | uint32_t seg_num, sec_bus, sub_bus; |
| 242 | uint32_t reg_value, dp_type, index = 0; |
| 243 | |
| 244 | dp_type = pcie_device_port_type(bdf); |
| 245 | |
| 246 | PCIE_DEBUG("DP type 0x%x\n", dp_type); |
| 247 | |
| 248 | /* If the device is RP or iEP_RP, set its rootport value to same */ |
| 249 | if ((dp_type == RP) || (dp_type == iEP_RP)) { |
| 250 | *rp_bdf = bdf; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | /* If the device is RCiEP and RCEC, set RP as 0xff */ |
| 255 | if ((dp_type == RCiEP) || (dp_type == RCEC)) { |
| 256 | *rp_bdf = 0xffffffff; |
| 257 | return 1; |
| 258 | } |
| 259 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 260 | assert(g_pcie_bdf_table != NULL); |
| 261 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 262 | while (index < g_pcie_bdf_table->num_entries) { |
| 263 | *rp_bdf = g_pcie_bdf_table->device[index++].bdf; |
| 264 | |
| 265 | /* |
| 266 | * Extract Secondary and Subordinate Bus numbers of the |
| 267 | * upstream Root port and check if the input function's |
| 268 | * bus number falls within that range. |
| 269 | */ |
| 270 | reg_value = pcie_read_cfg(*rp_bdf, TYPE1_PBN); |
| 271 | seg_num = PCIE_EXTRACT_BDF_SEG(*rp_bdf); |
| 272 | sec_bus = ((reg_value >> SECBN_SHIFT) & SECBN_MASK); |
| 273 | sub_bus = ((reg_value >> SUBBN_SHIFT) & SUBBN_MASK); |
| 274 | dp_type = pcie_device_port_type(*rp_bdf); |
| 275 | |
| 276 | if (((dp_type == RP) || (dp_type == iEP_RP)) && |
| 277 | (sec_bus <= PCIE_EXTRACT_BDF_BUS(bdf)) && |
| 278 | (sub_bus >= PCIE_EXTRACT_BDF_BUS(bdf)) && |
| 279 | (seg_num == PCIE_EXTRACT_BDF_SEG(bdf))) |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* Return failure */ |
| 284 | ERROR("PCIe Hierarchy fail: RP of bdf 0x%x not found\n", bdf); |
| 285 | *rp_bdf = 0; |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * @brief Sanity checks that all Endpoints must have a Rootport |
| 291 | * |
| 292 | * @param None |
| 293 | * @return 0 if sanity check passes, 1 if sanity check fails |
| 294 | */ |
| 295 | static uint32_t pcie_populate_device_rootport(void) |
| 296 | { |
| 297 | uint32_t bdf, rp_bdf; |
| 298 | pcie_device_bdf_table_t *bdf_tbl_ptr = g_pcie_bdf_table; |
| 299 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 300 | assert(bdf_tbl_ptr != NULL); |
| 301 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 302 | for (unsigned int tbl_index = 0; tbl_index < bdf_tbl_ptr->num_entries; |
| 303 | tbl_index++) { |
| 304 | bdf = bdf_tbl_ptr->device[tbl_index].bdf; |
| 305 | |
| 306 | /* Checks if the BDF has RootPort */ |
| 307 | pcie_get_rootport(bdf, &rp_bdf); |
| 308 | |
| 309 | bdf_tbl_ptr->device[tbl_index].rp_bdf = rp_bdf; |
| 310 | PCIE_DEBUG("Dev bdf: 0x%x RP bdf: 0x%x\n", bdf, rp_bdf); |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * @brief Returns the BDF Table pointer |
| 318 | * |
| 319 | * @param None |
| 320 | * |
| 321 | * @return BDF Table pointer |
| 322 | */ |
| 323 | pcie_device_bdf_table_t *pcie_get_bdf_table(void) |
| 324 | { |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 325 | assert(g_pcie_bdf_table != NULL); |
| 326 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 327 | return g_pcie_bdf_table; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * @brief This API creates the device bdf table from enumeration |
| 332 | * |
| 333 | * @param None |
| 334 | * |
| 335 | * @return None |
| 336 | */ |
| 337 | void pcie_create_device_bdf_table(void) |
| 338 | { |
| 339 | uint32_t seg_num, start_bus, end_bus; |
| 340 | uint32_t bus_index, dev_index, func_index, ecam_index; |
| 341 | uint32_t bdf, reg_value, cid_offset, status; |
| 342 | |
| 343 | assert(g_pcie_bdf_table != NULL); |
| 344 | |
| 345 | g_pcie_bdf_table->num_entries = 0; |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 346 | |
| 347 | assert(g_pcie_info_table != NULL); |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 348 | assert(g_pcie_info_table->num_entries != 0); |
| 349 | |
| 350 | for (ecam_index = 0; ecam_index < g_pcie_info_table->num_entries; ecam_index++) { |
| 351 | /* Derive ECAM specific information */ |
| 352 | const pcie_info_block_t *block = &g_pcie_info_table->block[ecam_index]; |
| 353 | |
| 354 | seg_num = block->segment_num; |
| 355 | start_bus = block->start_bus_num; |
| 356 | end_bus = block->end_bus_num; |
| 357 | |
| 358 | /* Iterate over all buses, devices and functions in this ecam */ |
| 359 | for (bus_index = start_bus; bus_index <= end_bus; bus_index++) { |
| 360 | for (dev_index = 0; dev_index < PCIE_MAX_DEV; dev_index++) { |
| 361 | for (func_index = 0; func_index < PCIE_MAX_FUNC; func_index++) { |
| 362 | /* Form BDF using seg, bus, device, function numbers */ |
| 363 | bdf = PCIE_CREATE_BDF(seg_num, bus_index, dev_index, |
| 364 | func_index); |
| 365 | |
| 366 | /* Probe PCIe device Function with this BDF */ |
| 367 | reg_value = pcie_read_cfg(bdf, TYPE01_VIDR); |
| 368 | |
| 369 | /* Store the Function's BDF if there was a valid response */ |
| 370 | if (reg_value != PCIE_UNKNOWN_RESPONSE) { |
| 371 | /* Skip if the device is a host bridge */ |
| 372 | if (pcie_is_host_bridge(bdf)) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | /* Skip if the device is a PCI legacy device */ |
| 377 | if (pcie_find_capability(bdf, PCIE_CAP, |
| 378 | CID_PCIECS, &cid_offset) != PCIE_SUCCESS) { |
| 379 | continue; |
| 380 | } |
| 381 | |
| 382 | status = pcie_check_device_valid(bdf); |
| 383 | if (!status) { |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | g_pcie_bdf_table->device[ |
| 388 | g_pcie_bdf_table->num_entries++].bdf = bdf; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* Sanity Check : Confirm all EP (normal, integrated) have a rootport */ |
| 396 | pcie_populate_device_rootport(); |
| 397 | INFO("Number of BDFs found : %u\n", g_pcie_bdf_table->num_entries); |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * @brief Returns the header type of the input pcie device function |
| 402 | * |
| 403 | * @param bdf - Segment/Bus/Dev/Func in the format of PCIE_CREATE_BDF |
| 404 | * @return TYPE0_HEADER for functions with Type 0 config space header, |
| 405 | * TYPE1_HEADER for functions with Type 1 config space header, |
| 406 | */ |
| 407 | uint32_t pcie_function_header_type(uint32_t bdf) |
| 408 | { |
| 409 | /* Read four bytes of config space starting from cache line size register */ |
| 410 | uint32_t reg_value = pcie_read_cfg(bdf, TYPE01_CLSR); |
| 411 | |
| 412 | /* Extract header type register value */ |
| 413 | reg_value = ((reg_value >> TYPE01_HTR_SHIFT) & TYPE01_HTR_MASK); |
| 414 | |
| 415 | /* Header layout bits within header type register indicate the header type */ |
| 416 | return ((reg_value >> HTR_HL_SHIFT) & HTR_HL_MASK); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * @brief Returns the ECAM address of the input PCIe function |
| 421 | * |
| 422 | * @param bdf - Segment/Bus/Dev/Func in PCIE_CREATE_BDF format |
| 423 | * @return ECAM address if success, else NULL address |
| 424 | */ |
| 425 | uintptr_t pcie_get_ecam_base(uint32_t bdf) |
| 426 | { |
| 427 | uint8_t ecam_index = 0, sec_bus = 0, sub_bus; |
| 428 | uint16_t seg_num = (uint16_t)PCIE_EXTRACT_BDF_SEG(bdf); |
| 429 | uint32_t reg_value; |
| 430 | uintptr_t ecam_base = 0; |
| 431 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 432 | assert(g_pcie_info_table != NULL); |
| 433 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 434 | while (ecam_index < g_pcie_info_table->num_entries) { |
| 435 | /* Derive ECAM specific information */ |
| 436 | const pcie_info_block_t *block = &g_pcie_info_table->block[ecam_index]; |
| 437 | |
| 438 | if (seg_num == block->segment_num) { |
| 439 | if (pcie_function_header_type(bdf) == TYPE0_HEADER) { |
| 440 | /* Return ecam_base if Type0 Header */ |
| 441 | ecam_base = block->ecam_base; |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | /* Check for Secondary/Subordinate bus if Type1 Header */ |
| 446 | reg_value = pcie_read_cfg(bdf, TYPE1_PBN); |
| 447 | sec_bus = ((reg_value >> SECBN_SHIFT) & SECBN_MASK); |
| 448 | sub_bus = ((reg_value >> SUBBN_SHIFT) & SUBBN_MASK); |
| 449 | |
| 450 | if ((sec_bus >= block->start_bus_num) && |
| 451 | (sub_bus <= block->end_bus_num)) { |
| 452 | ecam_base = block->ecam_base; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | ecam_index++; |
| 457 | } |
| 458 | |
| 459 | return ecam_base; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * @brief This API prints all the PCIe Devices info |
| 464 | * 1. Caller - Validation layer. |
| 465 | * 2. Prerequisite - val_pcie_create_info_table() |
| 466 | * @param None |
| 467 | * @return None |
| 468 | */ |
| 469 | void pcie_print_device_info(void) |
| 470 | { |
| 471 | uint32_t bdf, dp_type; |
| 472 | uint32_t tbl_index = 0; |
| 473 | uint32_t ecam_index = 0; |
| 474 | uint32_t ecam_base, ecam_start_bus, ecam_end_bus; |
| 475 | pcie_device_bdf_table_t *bdf_tbl_ptr = g_pcie_bdf_table; |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 476 | uint32_t num_rciep __unused = 0, num_rcec __unused = 0; |
| 477 | uint32_t num_iep __unused = 0, num_irp __unused = 0; |
| 478 | uint32_t num_ep __unused = 0, num_rp __unused = 0; |
| 479 | uint32_t num_dp __unused = 0, num_up __unused = 0; |
| 480 | uint32_t num_pcie_pci __unused = 0, num_pci_pcie __unused = 0; |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 481 | uint32_t bdf_counter; |
| 482 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 483 | assert(bdf_tbl_ptr != NULL); |
| 484 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 485 | if (bdf_tbl_ptr->num_entries == 0) { |
| 486 | INFO("BDF Table: No RCiEP or iEP found\n"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | for (tbl_index = 0; tbl_index < bdf_tbl_ptr->num_entries; tbl_index++) { |
| 491 | bdf = bdf_tbl_ptr->device[tbl_index].bdf; |
| 492 | dp_type = pcie_device_port_type(bdf); |
| 493 | |
| 494 | switch (dp_type) { |
| 495 | case RCiEP: |
| 496 | num_rciep++; |
| 497 | break; |
| 498 | case RCEC: |
| 499 | num_rcec++; |
| 500 | break; |
| 501 | case EP: |
| 502 | num_ep++; |
| 503 | break; |
| 504 | case RP: |
| 505 | num_rp++; |
| 506 | break; |
| 507 | case iEP_EP: |
| 508 | num_iep++; |
| 509 | break; |
| 510 | case iEP_RP: |
| 511 | num_irp++; |
| 512 | break; |
| 513 | case UP: |
| 514 | num_up++; |
| 515 | break; |
| 516 | case DP: |
| 517 | num_dp++; |
| 518 | break; |
| 519 | case PCI_PCIE: |
| 520 | num_pci_pcie++; |
| 521 | break; |
| 522 | case PCIE_PCI: |
| 523 | num_pcie_pci++; |
| 524 | break; |
| 525 | default: |
| 526 | ERROR("Unknown dp_type 0x%x\n", dp_type); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | INFO("Number of RCiEP : %u\n", num_rciep); |
| 531 | INFO("Number of RCEC : %u\n", num_rcec); |
| 532 | INFO("Number of EP : %u\n", num_ep); |
| 533 | INFO("Number of RP : %u\n", num_rp); |
| 534 | INFO("Number of iEP_EP : %u\n", num_iep); |
| 535 | INFO("Number of iEP_RP : %u\n", num_irp); |
| 536 | INFO("Number of UP of switch : %u\n", num_up); |
| 537 | INFO("Number of DP of switch : %u\n", num_dp); |
| 538 | INFO("Number of PCI/PCIe Bridge: %u\n", num_pci_pcie); |
| 539 | INFO("Number of PCIe/PCI Bridge: %u\n", num_pcie_pci); |
| 540 | |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 541 | assert(g_pcie_info_table != NULL); |
| 542 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 543 | while (ecam_index < g_pcie_info_table->num_entries) { |
| 544 | |
| 545 | /* Derive ECAM specific information */ |
| 546 | const pcie_info_block_t *block = &g_pcie_info_table->block[ecam_index]; |
| 547 | |
| 548 | ecam_base = block->ecam_base; |
| 549 | ecam_start_bus = block->start_bus_num; |
| 550 | ecam_end_bus = block->end_bus_num; |
| 551 | tbl_index = 0; |
| 552 | bdf_counter = 0; |
| 553 | |
| 554 | INFO("ECAM %u: base 0x%x\n", ecam_index, ecam_base); |
| 555 | |
| 556 | while (tbl_index < bdf_tbl_ptr->num_entries) { |
| 557 | uint32_t seg_num, bus_num, dev_num, func_num; |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 558 | uint32_t device_id __unused, vendor_id __unused, reg_value; |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 559 | uint32_t bdf, dev_ecam_base; |
| 560 | |
| 561 | bdf = bdf_tbl_ptr->device[tbl_index++].bdf; |
| 562 | seg_num = PCIE_EXTRACT_BDF_SEG(bdf); |
| 563 | bus_num = PCIE_EXTRACT_BDF_BUS(bdf); |
| 564 | dev_num = PCIE_EXTRACT_BDF_DEV(bdf); |
| 565 | func_num = PCIE_EXTRACT_BDF_FUNC(bdf); |
| 566 | |
| 567 | reg_value = pcie_read_cfg(bdf, TYPE01_VIDR); |
| 568 | device_id = (reg_value >> TYPE01_DIDR_SHIFT) & TYPE01_DIDR_MASK; |
| 569 | vendor_id = (reg_value >> TYPE01_VIDR_SHIFT) & TYPE01_VIDR_MASK; |
| 570 | |
| 571 | dev_ecam_base = pcie_get_ecam_base(bdf); |
| 572 | |
| 573 | if ((ecam_base == dev_ecam_base) && |
| 574 | (bus_num >= ecam_start_bus) && |
| 575 | (bus_num <= ecam_end_bus)) { |
| 576 | bdf_counter = 1; |
| 577 | bdf = PCIE_CREATE_BDF(seg_num, bus_num, dev_num, func_num); |
| 578 | INFO(" BDF: 0x%x\n", bdf); |
| 579 | INFO(" Seg: 0x%x Bus: 0x%x Dev: 0x%x " |
| 580 | "Func: 0x%x Dev ID: 0x%x Vendor ID: 0x%x\n", |
| 581 | seg_num, bus_num, dev_num, func_num, |
| 582 | device_id, vendor_id); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (bdf_counter == 0) { |
| 587 | INFO(" No BDF devices in ECAM region index %d\n", ecam_index); |
| 588 | } |
| 589 | |
| 590 | ecam_index++; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * @brief Create PCIe table and PCI enumeration |
| 596 | * @param void |
| 597 | * @return void |
| 598 | */ |
| 599 | void pcie_create_info_table(void) |
| 600 | { |
| 601 | unsigned int num_ecam; |
| 602 | |
| 603 | INFO("Creating PCIe info table\n"); |
| 604 | |
| 605 | g_pcie_info_table = plat_pcie_get_info_table(); |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 606 | if (g_pcie_info_table == NULL) { |
| 607 | ERROR("PCIe info not returned by platform\n"); |
| 608 | panic(); |
| 609 | } |
| 610 | |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 611 | g_pcie_bdf_table = pcie_bdf_table; |
| 612 | |
| 613 | num_ecam = g_pcie_info_table->num_entries; |
| 614 | INFO("Number of ECAM regions : %u\n", num_ecam); |
Soby Mathew | 2c2810f | 2024-11-15 17:11:24 +0000 | [diff] [blame] | 615 | if ((num_ecam == 0) || (num_ecam > MAX_PCIE_INFO_ENTRIES)) { |
| 616 | ERROR("PCIe info entries invalid\n"); |
| 617 | panic(); |
AlexeiFedorov | 9f0dc01 | 2024-09-10 10:22:06 +0100 | [diff] [blame] | 618 | } |
| 619 | pcie_create_device_bdf_table(); |
| 620 | pcie_print_device_info(); |
| 621 | } |
Soby Mathew | 5929bfe | 2024-11-28 12:28:00 +0000 | [diff] [blame] | 622 | |
| 623 | void pcie_init(void) |
| 624 | { |
| 625 | static bool is_init; |
| 626 | |
| 627 | /* Create PCIe table and enumeration */ |
| 628 | if (!is_init) { |
| 629 | pcie_create_info_table(); |
| 630 | is_init = true; |
| 631 | } |
| 632 | } |