1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef _PCI_MMIO_CFG_H
4 #define _PCI_MMIO_CFG_H
7 #include <device/mmio.h>
8 #include <device/pci_type.h>
10 /* Using a unique datatype for MMIO writes makes the pointers to _not_
11 * qualify for pointer aliasing with any other objects in memory.
13 * MMIO offset is a value originally derived from 'struct device *'
14 * in ramstage. For the compiler to not discard this MMIO offset value
15 * from CPU registers after any MMIO writes, -fstrict-aliasing has to
16 * be also set for the build.
18 * Bottom 12 bits (4 KiB) are reserved to address the registers of a
19 * single PCI function. Declare the bank as a union to avoid some casting
20 * in the functions below.
24 uint16_t reg16
[4096 / sizeof(uint16_t)];
25 uint32_t reg32
[4096 / sizeof(uint32_t)];
28 #if CONFIG(ECAM_MMCONF_SUPPORT)
30 #if CONFIG_ECAM_MMCONF_BASE_ADDRESS == 0
31 #error "CONFIG_ECAM_MMCONF_BASE_ADDRESS undefined!"
34 #if CONFIG_ECAM_MMCONF_BUS_NUMBER == 0
35 #error "CONFIG_ECAM_MMCONF_BUS_NUMBER is undefined!"
38 #if CONFIG_ECAM_MMCONF_BUS_NUMBER * MiB != CONFIG_ECAM_MMCONF_LENGTH
39 #error "CONFIG_ECAM_MMCONF_LENGTH does not correspond with CONFIG_ECAM_MMCONF_BUS_NUMBER!"
42 /* By not assigning this to CONFIG_ECAM_MMCONF_BASE_ADDRESS here we
43 prevent some sub-optimal constant folding. */
44 extern u8
*const pci_mmconf
;
46 static __always_inline
47 volatile union pci_bank
*pci_map_bus(pci_devfn_t dev
)
49 return (void *)&pci_mmconf
[PCI_DEVFN_OFFSET(dev
)];
54 /* For platforms not supporting ECAM, they need to define pci_map_bus function
55 * in their platform-specific code */
56 volatile union pci_bank
*pci_map_bus(pci_devfn_t dev
);
61 * Avoid name collisions as different stages have different signature
62 * for these functions. The _s_ stands for simple, fundamental IO or
66 static __always_inline
67 uint8_t pci_s_read_config8(pci_devfn_t dev
, uint16_t reg
)
69 return pci_map_bus(dev
)->reg8
[reg
];
72 static __always_inline
73 uint16_t pci_s_read_config16(pci_devfn_t dev
, uint16_t reg
)
75 return pci_map_bus(dev
)->reg16
[reg
/ sizeof(uint16_t)];
78 static __always_inline
79 uint32_t pci_s_read_config32(pci_devfn_t dev
, uint16_t reg
)
81 return pci_map_bus(dev
)->reg32
[reg
/ sizeof(uint32_t)];
84 static __always_inline
85 void pci_s_write_config8(pci_devfn_t dev
, uint16_t reg
, uint8_t value
)
87 pci_map_bus(dev
)->reg8
[reg
] = value
;
90 static __always_inline
91 void pci_s_write_config16(pci_devfn_t dev
, uint16_t reg
, uint16_t value
)
93 pci_map_bus(dev
)->reg16
[reg
/ sizeof(uint16_t)] = value
;
96 static __always_inline
97 void pci_s_write_config32(pci_devfn_t dev
, uint16_t reg
, uint32_t value
)
99 pci_map_bus(dev
)->reg32
[reg
/ sizeof(uint32_t)] = value
;
103 * The functions pci_mmio_config*_addr provide a way to determine the MMIO address of a PCI
104 * config register. The address returned is dependent of both the MMCONF base address and the
105 * assigned PCI bus number of the requested device, which both can change during the boot
106 * process. Thus, the pointer returned here must not be cached!
108 static __always_inline
109 uint8_t *pci_mmio_config8_addr(pci_devfn_t dev
, uint16_t reg
)
111 return (uint8_t *)&pci_map_bus(dev
)->reg8
[reg
];
114 static __always_inline
115 uint16_t *pci_mmio_config16_addr(pci_devfn_t dev
, uint16_t reg
)
117 return (uint16_t *)&pci_map_bus(dev
)->reg16
[reg
/ sizeof(uint16_t)];
120 static __always_inline
121 uint32_t *pci_mmio_config32_addr(pci_devfn_t dev
, uint16_t reg
)
123 return (uint32_t *)&pci_map_bus(dev
)->reg32
[reg
/ sizeof(uint32_t)];
126 #endif /* _PCI_MMIO_CFG_H */