Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 2 | |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 3 | #ifndef _PCI_MMIO_CFG_H |
| 4 | #define _PCI_MMIO_CFG_H |
| 5 | |
Kyösti Mälkki | 8fd78a6 | 2019-01-23 15:59:38 +0200 | [diff] [blame] | 6 | #include <stdint.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 7 | #include <device/mmio.h> |
Kyösti Mälkki | 8fd78a6 | 2019-01-23 15:59:38 +0200 | [diff] [blame] | 8 | #include <device/pci_type.h> |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 9 | |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 10 | /* 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 | */ |
| 22 | union 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 | |
Shelley Chen | 4e9bb33 | 2021-10-20 15:43:45 -0700 | [diff] [blame] | 28 | #if CONFIG(ECAM_MMCONF_SUPPORT) |
Nico Huber | e01e25d | 2021-10-14 18:22:30 +0200 | [diff] [blame] | 29 | |
Shelley Chen | 4e9bb33 | 2021-10-20 15:43:45 -0700 | [diff] [blame] | 30 | #if CONFIG_ECAM_MMCONF_BASE_ADDRESS == 0 |
| 31 | #error "CONFIG_ECAM_MMCONF_BASE_ADDRESS undefined!" |
Nico Huber | e01e25d | 2021-10-14 18:22:30 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Felix Held | 20c9c55 | 2024-01-12 20:56:48 +0100 | [diff] [blame^] | 34 | #if CONFIG_ECAM_MMCONF_BUS_NUMBER == 0 |
| 35 | #error "CONFIG_ECAM_MMCONF_BUS_NUMBER is undefined!" |
| 36 | #endif |
| 37 | |
Shelley Chen | 4e9bb33 | 2021-10-20 15:43:45 -0700 | [diff] [blame] | 38 | #if CONFIG_ECAM_MMCONF_BUS_NUMBER * MiB != CONFIG_ECAM_MMCONF_LENGTH |
| 39 | #error "CONFIG_ECAM_MMCONF_LENGTH does not correspond with CONFIG_ECAM_MMCONF_BUS_NUMBER!" |
Nico Huber | e01e25d | 2021-10-14 18:22:30 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
Shelley Chen | 4e9bb33 | 2021-10-20 15:43:45 -0700 | [diff] [blame] | 42 | /* By not assigning this to CONFIG_ECAM_MMCONF_BASE_ADDRESS here we |
Nico Huber | e01e25d | 2021-10-14 18:22:30 +0200 | [diff] [blame] | 43 | prevent some sub-optimal constant folding. */ |
| 44 | extern u8 *const pci_mmconf; |
| 45 | |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 46 | static __always_inline |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 47 | volatile union pci_bank *pci_map_bus(pci_devfn_t dev) |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame] | 48 | { |
| 49 | return (void *)&pci_mmconf[PCI_DEVFN_OFFSET(dev)]; |
| 50 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 51 | |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 52 | #else |
| 53 | |
| 54 | /* For platforms not supporting ECAM, they need to define pci_map_bus function |
| 55 | * in their platform-specific code */ |
| 56 | volatile union pci_bank *pci_map_bus(pci_devfn_t dev); |
| 57 | |
Nico Huber | e01e25d | 2021-10-14 18:22:30 +0200 | [diff] [blame] | 58 | #endif |
| 59 | |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Avoid name collisions as different stages have different signature |
| 62 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 63 | * MMIO variant. |
| 64 | */ |
| 65 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 66 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 67 | uint8_t pci_s_read_config8(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 68 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 69 | return pci_map_bus(dev)->reg8[reg]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 72 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 73 | uint16_t pci_s_read_config16(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 74 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 75 | return pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 78 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 79 | uint32_t pci_s_read_config32(pci_devfn_t dev, uint16_t reg) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 80 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 81 | return pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 84 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 85 | void pci_s_write_config8(pci_devfn_t dev, uint16_t reg, uint8_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 86 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 87 | pci_map_bus(dev)->reg8[reg] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 90 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 91 | void pci_s_write_config16(pci_devfn_t dev, uint16_t reg, uint16_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 92 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 93 | pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 96 | static __always_inline |
Nico Huber | f4f365f | 2021-10-14 18:16:39 +0200 | [diff] [blame] | 97 | void pci_s_write_config32(pci_devfn_t dev, uint16_t reg, uint32_t value) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 98 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 99 | pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)] = value; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Michael Niewöhner | efe3cfb | 2019-11-15 22:47:33 +0100 | [diff] [blame] | 102 | /* |
| 103 | * The functions pci_mmio_config*_addr provide a way to determine the MMIO address of a PCI |
| 104 | * config register. The address returned is dependent of both the MMCONF base address and the |
| 105 | * assigned PCI bus number of the requested device, which both can change during the boot |
| 106 | * process. Thus, the pointer returned here must not be cached! |
| 107 | */ |
Michael Niewöhner | 8f22136 | 2019-11-08 22:02:02 +0100 | [diff] [blame] | 108 | static __always_inline |
| 109 | uint8_t *pci_mmio_config8_addr(pci_devfn_t dev, uint16_t reg) |
| 110 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 111 | return (uint8_t *)&pci_map_bus(dev)->reg8[reg]; |
Michael Niewöhner | 8f22136 | 2019-11-08 22:02:02 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static __always_inline |
| 115 | uint16_t *pci_mmio_config16_addr(pci_devfn_t dev, uint16_t reg) |
| 116 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 117 | return (uint16_t *)&pci_map_bus(dev)->reg16[reg / sizeof(uint16_t)]; |
Michael Niewöhner | 8f22136 | 2019-11-08 22:02:02 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static __always_inline |
| 121 | uint32_t *pci_mmio_config32_addr(pci_devfn_t dev, uint16_t reg) |
| 122 | { |
Jianjun Wang | 75a7c6e | 2021-11-27 14:11:02 +0800 | [diff] [blame] | 123 | return (uint32_t *)&pci_map_bus(dev)->reg32[reg / sizeof(uint32_t)]; |
Michael Niewöhner | 8f22136 | 2019-11-08 22:02:02 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 126 | #endif /* _PCI_MMIO_CFG_H */ |