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