blob: 557adcb0ca9fa3aeb1d174a191a83b5a8bc18dd2 [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
Nico Hubere01e25d2021-10-14 18:22:30 +020028#if CONFIG(MMCONF_SUPPORT)
29
30#if CONFIG_MMCONF_BASE_ADDRESS == 0
31#error "CONFIG_MMCONF_BASE_ADDRESS undefined!"
32#endif
33
34#if CONFIG_MMCONF_BUS_NUMBER * MiB != CONFIG_MMCONF_LENGTH
35#error "CONFIG_MMCONF_LENGTH does not correspond with CONFIG_MMCONF_BUS_NUMBER!"
36#endif
37
38/* By not assigning this to CONFIG_MMCONF_BASE_ADDRESS here we
39 prevent some sub-optimal constant folding. */
40extern u8 *const pci_mmconf;
41
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020042static __always_inline
43volatile union pci_bank *pcicfg(pci_devfn_t dev)
44{
45 return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)];
46}
Stefan Reinauer00636b02012-04-04 00:08:51 +020047
Nico Hubere01e25d2021-10-14 18:22:30 +020048#endif
49
Aaron Durbin75a62e72018-09-13 02:10:45 -060050static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020051uint8_t pci_mmio_read_config8(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020052{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020053 return pcicfg(dev)->reg8[reg];
Stefan Reinauer00636b02012-04-04 00:08:51 +020054}
55
Aaron Durbin75a62e72018-09-13 02:10:45 -060056static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020057uint16_t pci_mmio_read_config16(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020058{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020059 return pcicfg(dev)->reg16[reg / sizeof(uint16_t)];
Stefan Reinauer00636b02012-04-04 00:08:51 +020060}
61
Aaron Durbin75a62e72018-09-13 02:10:45 -060062static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020063uint32_t pci_mmio_read_config32(pci_devfn_t dev, uint16_t reg)
Stefan Reinauer00636b02012-04-04 00:08:51 +020064{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020065 return pcicfg(dev)->reg32[reg / sizeof(uint32_t)];
Stefan Reinauer00636b02012-04-04 00:08:51 +020066}
67
Aaron Durbin75a62e72018-09-13 02:10:45 -060068static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020069void pci_mmio_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020070{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020071 pcicfg(dev)->reg8[reg] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +020072}
73
Aaron Durbin75a62e72018-09-13 02:10:45 -060074static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020075void pci_mmio_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020076{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020077 pcicfg(dev)->reg16[reg / sizeof(uint16_t)] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +020078}
79
Aaron Durbin75a62e72018-09-13 02:10:45 -060080static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +020081void pci_mmio_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value)
Stefan Reinauer00636b02012-04-04 00:08:51 +020082{
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020083 pcicfg(dev)->reg32[reg / sizeof(uint32_t)] = value;
Stefan Reinauer00636b02012-04-04 00:08:51 +020084}
85
Michael Niewöhnerefe3cfb2019-11-15 22:47:33 +010086/*
87 * The functions pci_mmio_config*_addr provide a way to determine the MMIO address of a PCI
88 * config register. The address returned is dependent of both the MMCONF base address and the
89 * assigned PCI bus number of the requested device, which both can change during the boot
90 * process. Thus, the pointer returned here must not be cached!
91 */
Michael Niewöhner8f221362019-11-08 22:02:02 +010092static __always_inline
93uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg)
94{
95 return (uint8_t *)&pcicfg(dev)->reg8[reg];
96}
97
98static __always_inline
99uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg)
100{
101 return (uint16_t *)&pcicfg(dev)->reg16[reg / sizeof(uint16_t)];
102}
103
104static __always_inline
105uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg)
106{
107 return (uint32_t *)&pcicfg(dev)->reg32[reg / sizeof(uint32_t)];
108}
109
Kyösti Mälkki92b52962019-03-01 08:08:28 +0200110/* Avoid name collisions as different stages have different signature
111 * for these functions. The _s_ stands for simple, fundamental IO or
112 * MMIO variant.
113 */
114
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200115static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200116uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200117{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200118 return pci_mmio_read_config8(dev, reg);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200119}
120
121static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200122uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200123{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200124 return pci_mmio_read_config16(dev, reg);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200125}
126
127static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200128uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200129{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200130 return pci_mmio_read_config32(dev, reg);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200131}
132
133static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200134void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200135{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200136 pci_mmio_write_config8(dev, reg, value);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200137}
138
139static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200140void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200141{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200142 pci_mmio_write_config16(dev, reg, value);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200143}
144
145static __always_inline
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200146void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value)
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200147{
Kyösti Mälkkib603fdc2019-03-11 20:33:01 +0200148 pci_mmio_write_config32(dev, reg, value);
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200149}
Kyösti Mälkki2d8aff32019-01-23 16:44:55 +0200150
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +0300151#endif /* _PCI_MMIO_CFG_H */