Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 3 | |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 4 | #ifndef _PCI_MMIO_CFG_H |
| 5 | #define _PCI_MMIO_CFG_H |
| 6 | |
Kyösti Mälkki | 8fd78a6 | 2019-01-23 15:59:38 +0200 | [diff] [blame] | 7 | #include <stdint.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 8 | #include <device/mmio.h> |
Kyösti Mälkki | 8fd78a6 | 2019-01-23 15:59:38 +0200 | [diff] [blame] | 9 | #include <device/pci_type.h> |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 10 | |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 11 | |
| 12 | /* By not assigning this to CONFIG_MMCONF_BASE_ADDRESS here we |
| 13 | * prevent some sub-optimal constant folding. */ |
| 14 | extern u8 *const pci_mmconf; |
| 15 | |
| 16 | /* Using a unique datatype for MMIO writes makes the pointers to _not_ |
| 17 | * qualify for pointer aliasing with any other objects in memory. |
| 18 | * |
| 19 | * MMIO offset is a value originally derived from 'struct device *' |
| 20 | * in ramstage. For the compiler to not discard this MMIO offset value |
| 21 | * from CPU registers after any MMIO writes, -fstrict-aliasing has to |
| 22 | * be also set for the build. |
| 23 | * |
| 24 | * Bottom 12 bits (4 KiB) are reserved to address the registers of a |
| 25 | * single PCI function. Declare the bank as a union to avoid some casting |
| 26 | * in the functions below. |
| 27 | */ |
| 28 | union pci_bank { |
| 29 | uint8_t reg8[4096]; |
| 30 | uint16_t reg16[4096 / sizeof(uint16_t)]; |
| 31 | uint32_t reg32[4096 / sizeof(uint32_t)]; |
| 32 | }; |
| 33 | |
| 34 | static __always_inline |
| 35 | volatile union pci_bank *pcicfg(pci_devfn_t dev) |
| 36 | { |
| 37 | return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)]; |
| 38 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 39 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 40 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 41 | uint8_t pci_mmio_read_config8(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 42 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 43 | return pcicfg(dev)->reg8[reg]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 46 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 47 | uint16_t pci_mmio_read_config16(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 48 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 49 | return pcicfg(dev)->reg16[reg / sizeof(uint16_t)]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 52 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 53 | uint32_t pci_mmio_read_config32(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 54 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 55 | return pcicfg(dev)->reg32[reg / sizeof(uint32_t)]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 58 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 59 | void pci_mmio_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 60 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 61 | pcicfg(dev)->reg8[reg] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 64 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 65 | void pci_mmio_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 66 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 67 | pcicfg(dev)->reg16[reg / sizeof(uint16_t)] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 70 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 71 | void pci_mmio_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 72 | { |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 73 | pcicfg(dev)->reg32[reg / sizeof(uint32_t)] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Michael Niewöhner | efe3cfb | 2019-11-15 22:47:33 +0100 | [diff] [blame] | 76 | /* |
| 77 | * The functions pci_mmio_config*_addr provide a way to determine the MMIO address of a PCI |
| 78 | * config register. The address returned is dependent of both the MMCONF base address and the |
| 79 | * assigned PCI bus number of the requested device, which both can change during the boot |
| 80 | * process. Thus, the pointer returned here must not be cached! |
| 81 | */ |
Michael Niewöhner | 8f22136 | 2019-11-08 22:02:02 +0100 | [diff] [blame] | 82 | static __always_inline |
| 83 | uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg) |
| 84 | { |
| 85 | return (uint8_t *)&pcicfg(dev)->reg8[reg]; |
| 86 | } |
| 87 | |
| 88 | static __always_inline |
| 89 | uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg) |
| 90 | { |
| 91 | return (uint16_t *)&pcicfg(dev)->reg16[reg / sizeof(uint16_t)]; |
| 92 | } |
| 93 | |
| 94 | static __always_inline |
| 95 | uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg) |
| 96 | { |
| 97 | return (uint32_t *)&pcicfg(dev)->reg32[reg / sizeof(uint32_t)]; |
| 98 | } |
| 99 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 100 | #if CONFIG(MMCONF_SUPPORT) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 101 | |
Arthur Heymans | 15fcc86 | 2019-10-08 11:29:12 +0200 | [diff] [blame] | 102 | #if CONFIG_MMCONF_BASE_ADDRESS == 0 |
| 103 | #error "CONFIG_MMCONF_BASE_ADDRESS undefined!" |
| 104 | #endif |
| 105 | |
Kyösti Mälkki | 92b5296 | 2019-03-01 08:08:28 +0200 | [diff] [blame] | 106 | /* Avoid name collisions as different stages have different signature |
| 107 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 108 | * MMIO variant. |
| 109 | */ |
| 110 | |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 111 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 112 | uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 113 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 114 | return pci_mmio_read_config8(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 118 | uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 119 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 120 | return pci_mmio_read_config16(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 124 | uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 125 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 126 | return pci_mmio_read_config32(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 130 | void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 131 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 132 | pci_mmio_write_config8(dev, reg, value); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 136 | void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 137 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 138 | pci_mmio_write_config16(dev, reg, value); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 142 | void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 143 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 144 | pci_mmio_write_config32(dev, reg, value); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 145 | } |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 146 | |
| 147 | #endif |
| 148 | |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 149 | #endif /* _PCI_MMIO_CFG_H */ |