blob: aa3911fde16ec0caf416166662f2ba469a266912 [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#ifndef PCIE_H
8#define PCIE_H
9
10#include <cdefs.h>
11#include <stdint.h>
12#include <utils_def.h>
13
14typedef struct {
15 unsigned long ecam_base; /* ECAM base address */
16 unsigned int segment_num; /* Segment number of this ECAM */
17 unsigned int start_bus_num; /* Start bus number for this ECAM space */
18 unsigned int end_bus_num; /* Last bus number */
19} pcie_info_block_t;
20
21typedef struct {
22 unsigned int num_entries; /* Number of entries */
23 pcie_info_block_t block[];
24} pcie_info_table_t;
25
26typedef struct {
27 uint32_t bdf;
28 uint32_t rp_bdf;
29} pcie_device_attr_t;
30
31typedef struct __packed {
32 uint32_t num_entries;
33 pcie_device_attr_t device[]; /* in the format of Segment/Bus/Dev/Func */
34} pcie_device_bdf_table_t;
35
36/* Address initialisation structure */
37typedef struct {
38 /* 64 bit prefetchable memory start address */
39 uint64_t bar64_p_start;
40 uint64_t rp_bar64_value;
41 /* 32 bit non-prefetchable memory start address */
42 uint32_t bar32_np_start;
43 /* 32 bit prefetchable memory start address */
44 uint32_t bar32_p_start;
45 uint32_t rp_bar32_value;
46} pcie_bar_init_t;
47
48#define PCIE_EXTRACT_BDF_SEG(bdf) ((bdf >> 24) & 0xFF)
49#define PCIE_EXTRACT_BDF_BUS(bdf) ((bdf >> 16) & 0xFF)
50#define PCIE_EXTRACT_BDF_DEV(bdf) ((bdf >> 8) & 0xFF)
51#define PCIE_EXTRACT_BDF_FUNC(bdf) (bdf & 0xFF)
52
53/* PCI-compatible region */
54#define PCI_CMP_CFG_SIZE 256
55
56/* PCI Express Extended Configuration Space */
57#define PCIE_CFG_SIZE 4096
58
59#define PCIE_MAX_BUS 256
60#define PCIE_MAX_DEV 32
61#define PCIE_MAX_FUNC 8
62
63#define PCIE_CREATE_BDF(Seg, Bus, Dev, Func) \
64 ((Seg << 24) | (Bus << 16) | (Dev << 8) | Func)
65
66#define PCIE_SUCCESS 0x00000000 /* Operation completed successfully */
67#define PCIE_NO_MAPPING 0x10000001 /* A mapping to a Function does not exist */
68#define PCIE_CAP_NOT_FOUND 0x10000010 /* The specified capability was not found */
69#define PCIE_UNKNOWN_RESPONSE 0xFFFFFFFF /* Function not found or UR response from completer */
70
71/* Allows storage of 2048 valid BDFs */
72#define PCIE_DEVICE_BDF_TABLE_SZ 8192
73
74typedef enum {
75 HEADER = 0,
76 PCIE_CAP = 1,
77 PCIE_ECAP = 2
78} bitfield_reg_type_t;
79
80typedef enum {
81 HW_INIT = 0,
82 READ_ONLY = 1,
83 STICKY_RO = 2,
84 RSVDP_RO = 3,
85 RSVDZ_RO = 4,
86 READ_WRITE = 5,
87 STICKY_RW = 6
88} bitfield_attr_type_t;
89
90/* Class Code Masks */
91#define CC_SUB_MASK 0xFF /* Sub Class */
92#define CC_BASE_MASK 0xFF /* Base Class */
93
94/* Class Code Shifts */
95#define CC_SHIFT 8
96#define CC_SUB_SHIFT 16
97#define CC_BASE_SHIFT 24
98
99void pcie_create_info_table(void);
100pcie_device_bdf_table_t *pcie_get_bdf_table(void);
101uint32_t pcie_find_capability(uint32_t bdf, uint32_t cid_type, uint32_t cid,
102 uint32_t *cid_offset);
103uint32_t pcie_read_cfg(uint32_t bdf, uint32_t offset);
104void pcie_write_cfg(uint32_t bdf, uint32_t offset, uint32_t data);
105
106#endif /* PCIE_H */