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