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