2020-04-02 23:48:27 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2012-04-04 00:08:51 +02:00
|
|
|
|
2013-06-19 22:05:00 +02:00
|
|
|
#ifndef _PCI_MMIO_CFG_H
|
|
|
|
#define _PCI_MMIO_CFG_H
|
|
|
|
|
2019-01-23 14:59:38 +01:00
|
|
|
#include <stdint.h>
|
2019-03-03 07:01:05 +01:00
|
|
|
#include <device/mmio.h>
|
2019-01-23 14:59:38 +01:00
|
|
|
#include <device/pci_type.h>
|
2013-06-19 22:05:00 +02:00
|
|
|
|
2019-03-05 06:56:38 +01:00
|
|
|
/* By not assigning this to CONFIG_MMCONF_BASE_ADDRESS here we
|
|
|
|
* prevent some sub-optimal constant folding. */
|
|
|
|
extern u8 *const pci_mmconf;
|
|
|
|
|
|
|
|
/* Using a unique datatype for MMIO writes makes the pointers to _not_
|
|
|
|
* qualify for pointer aliasing with any other objects in memory.
|
|
|
|
*
|
|
|
|
* MMIO offset is a value originally derived from 'struct device *'
|
|
|
|
* in ramstage. For the compiler to not discard this MMIO offset value
|
|
|
|
* from CPU registers after any MMIO writes, -fstrict-aliasing has to
|
|
|
|
* be also set for the build.
|
|
|
|
*
|
|
|
|
* Bottom 12 bits (4 KiB) are reserved to address the registers of a
|
|
|
|
* single PCI function. Declare the bank as a union to avoid some casting
|
|
|
|
* in the functions below.
|
|
|
|
*/
|
|
|
|
union pci_bank {
|
|
|
|
uint8_t reg8[4096];
|
|
|
|
uint16_t reg16[4096 / sizeof(uint16_t)];
|
|
|
|
uint32_t reg32[4096 / sizeof(uint32_t)];
|
|
|
|
};
|
|
|
|
|
|
|
|
static __always_inline
|
|
|
|
volatile union pci_bank *pcicfg(pci_devfn_t dev)
|
|
|
|
{
|
|
|
|
return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)];
|
|
|
|
}
|
2012-04-04 00:08:51 +02:00
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint8_t pci_mmio_read_config8(pci_devfn_t dev, uint16_t reg)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
return pcicfg(dev)->reg8[reg];
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint16_t pci_mmio_read_config16(pci_devfn_t dev, uint16_t reg)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
return pcicfg(dev)->reg16[reg / sizeof(uint16_t)];
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint32_t pci_mmio_read_config32(pci_devfn_t dev, uint16_t reg)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
return pcicfg(dev)->reg32[reg / sizeof(uint32_t)];
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_mmio_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
pcicfg(dev)->reg8[reg] = value;
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_mmio_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
pcicfg(dev)->reg16[reg / sizeof(uint16_t)] = value;
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:10:45 +02:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_mmio_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value)
|
2012-04-04 00:08:51 +02:00
|
|
|
{
|
2019-03-05 06:56:38 +01:00
|
|
|
pcicfg(dev)->reg32[reg / sizeof(uint32_t)] = value;
|
2012-04-04 00:08:51 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:47:33 +01:00
|
|
|
/*
|
|
|
|
* The functions pci_mmio_config*_addr provide a way to determine the MMIO address of a PCI
|
|
|
|
* config register. The address returned is dependent of both the MMCONF base address and the
|
|
|
|
* assigned PCI bus number of the requested device, which both can change during the boot
|
|
|
|
* process. Thus, the pointer returned here must not be cached!
|
|
|
|
*/
|
2019-11-08 22:02:02 +01:00
|
|
|
static __always_inline
|
|
|
|
uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg)
|
|
|
|
{
|
|
|
|
return (uint8_t *)&pcicfg(dev)->reg8[reg];
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
|
|
|
uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg)
|
|
|
|
{
|
|
|
|
return (uint16_t *)&pcicfg(dev)->reg16[reg / sizeof(uint16_t)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
|
|
|
uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg)
|
|
|
|
{
|
|
|
|
return (uint32_t *)&pcicfg(dev)->reg32[reg / sizeof(uint32_t)];
|
|
|
|
}
|
|
|
|
|
2019-03-06 01:53:33 +01:00
|
|
|
#if CONFIG(MMCONF_SUPPORT)
|
2019-01-23 15:44:55 +01:00
|
|
|
|
2019-10-08 11:29:12 +02:00
|
|
|
#if CONFIG_MMCONF_BASE_ADDRESS == 0
|
|
|
|
#error "CONFIG_MMCONF_BASE_ADDRESS undefined!"
|
2021-01-28 13:04:06 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MMCONF_BUS_NUMBER * MiB != CONFIG_MMCONF_LENGTH
|
|
|
|
#error "CONFIG_MMCONF_LENGTH does not correspond with CONFIG_MMCONF_BUS_NUMBER!"
|
2019-10-08 11:29:12 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-01 07:08:28 +01:00
|
|
|
/* Avoid name collisions as different stages have different signature
|
|
|
|
* for these functions. The _s_ stands for simple, fundamental IO or
|
|
|
|
* MMIO variant.
|
|
|
|
*/
|
|
|
|
|
2019-01-23 15:44:55 +01:00
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
return pci_mmio_read_config8(dev, reg);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
return pci_mmio_read_config16(dev, reg);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
return pci_mmio_read_config32(dev, reg);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
pci_mmio_write_config8(dev, reg, value);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
pci_mmio_write_config16(dev, reg, value);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline
|
2019-03-11 19:33:01 +01:00
|
|
|
void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value)
|
2019-01-23 15:44:55 +01:00
|
|
|
{
|
2019-03-11 19:33:01 +01:00
|
|
|
pci_mmio_write_config32(dev, reg, value);
|
2019-01-23 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-06-19 22:05:00 +02:00
|
|
|
#endif /* _PCI_MMIO_CFG_H */
|