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