blob: 416444179ff19c32e8746883092fbd20f7fb8820 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer00636b02012-04-04 00:08:51 +02002
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +03003#ifndef _PCI_MMIO_CFG_H
4#define _PCI_MMIO_CFG_H
5
Kyösti Mälkki8fd78a62019-01-23 15:59:38 +02006#include <stdint.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Kyösti Mälkki8fd78a62019-01-23 15:59:38 +02008#include <device/pci_type.h>
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +03009
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020010/* Using a unique datatype for MMIO writes makes the pointers to _not_
11 * qualify for pointer aliasing with any other objects in memory.
12 *
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.
17 *
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.
21 */
22union pci_bank {
23 uint8_t reg8[4096];
24 uint16_t reg16[4096 / sizeof(uint16_t)];
25 uint32_t reg32[4096 / sizeof(uint32_t)];
26};
27
Shelley Chen4e9bb332021-10-20 15:43:45 -070028#if CONFIG(ECAM_MMCONF_SUPPORT)
Nico Hubere01e25d2021-10-14 18:22:30 +020029
Shelley Chen4e9bb332021-10-20 15:43:45 -070030#if CONFIG_ECAM_MMCONF_BASE_ADDRESS == 0
31#error "CONFIG_ECAM_MMCONF_BASE_ADDRESS undefined!"
Nico Hubere01e25d2021-10-14 18:22:30 +020032#endif
33
Felix Held20c9c552024-01-12 20:56:48 +010034#if CONFIG_ECAM_MMCONF_BUS_NUMBER == 0
35#error "CONFIG_ECAM_MMCONF_BUS_NUMBER is undefined!"
36#endif
37
Shelley Chen4e9bb332021-10-20 15:43:45 -070038#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!"
Nico Hubere01e25d2021-10-14 18:22:30 +020040#endif
41
Shelley Chen4e9bb332021-10-20 15:43:45 -070042/* By not assigning this to CONFIG_ECAM_MMCONF_BASE_ADDRESS here we
Nico Hubere01e25d2021-10-14 18:22:30 +020043 prevent some sub-optimal constant folding. */
44extern u8 *const pci_mmconf;
45
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020046static __always_inline
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080047volatile union pci_bank *pci_map_bus(pci_devfn_t dev)
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020048{
49 return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)];
50}
Stefan Reinauer00636b02012-04-04 00:08:51 +020051
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080052#else
53
54/* For platforms not supporting ECAM, they need to define pci_map_bus function
55 * in their platform-specific code */
56volatile union pci_bank *pci_map_bus(pci_devfn_t dev);
57
Nico Hubere01e25d2021-10-14 18:22:30 +020058#endif
59
Nico Huberf4f365f2021-10-14 18:16:39 +020060/*
61 * Avoid name collisions as different stages have different signature
62 * for these functions. The _s_ stands for simple, fundamental IO or
63 * MMIO variant.
64 */
65
Aaron Durbin75a62e72018-09-13 02:10:45 -060066static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020067uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020068{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080069 return pci_map_bus(dev)->reg8[reg];
Stefan Reinauer00636b02012-04-04 00:08:51 +020070}
71
Aaron Durbin75a62e72018-09-13 02:10:45 -060072static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020073uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020074{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080075 return pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)];
Stefan Reinauer00636b02012-04-04 00:08:51 +020076}
77
Aaron Durbin75a62e72018-09-13 02:10:45 -060078static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020079uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020080{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080081 return pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)];
Stefan Reinauer00636b02012-04-04 00:08:51 +020082}
83
Aaron Durbin75a62e72018-09-13 02:10:45 -060084static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020085void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020086{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080087 pci_map_bus(dev)->reg8[reg] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +020088}
89
Aaron Durbin75a62e72018-09-13 02:10:45 -060090static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020091void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020092{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080093 pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +020094}
95
Aaron Durbin75a62e72018-09-13 02:10:45 -060096static __always_inline
Nico Huberf4f365f2021-10-14 18:16:39 +020097void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020098{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +080099 pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200100}
101
Michael Niewöhnerefe3cfb2019-11-15 22:47:33 +0100102/*
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!
107 */
Michael Niewöhner8f221362019-11-08 22:02:02 +0100108static __always_inline
109uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg)
110{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +0800111 return (uint8_t *)&pci_map_bus(dev)->reg8[reg];
Michael Niewöhner8f221362019-11-08 22:02:02 +0100112}
113
114static __always_inline
115uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg)
116{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +0800117 return (uint16_t *)&pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)];
Michael Niewöhner8f221362019-11-08 22:02:02 +0100118}
119
120static __always_inline
121uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg)
122{
Jianjun Wang75a7c6e2021-11-27 14:11:02 +0800123 return (uint32_t *)&pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)];
Michael Niewöhner8f221362019-11-08 22:02:02 +0100124}
125
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +0300126#endif /* _PCI_MMIO_CFG_H */