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 | |
Kyösti Mälkki | d2cdfff | 2019-03-05 07:56:38 +0200 | [diff] [blame^] | 89 | #endif /* !defined(__ROMCC__) */ |
| 90 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 91 | #if CONFIG(MMCONF_SUPPORT) |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 92 | |
Kyösti Mälkki | 92b5296 | 2019-03-01 08:08:28 +0200 | [diff] [blame] | 93 | /* Avoid name collisions as different stages have different signature |
| 94 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 95 | * MMIO variant. |
| 96 | */ |
| 97 | |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 98 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 99 | 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] | 100 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 101 | return pci_mmio_read_config8(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 107 | return pci_mmio_read_config16(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 111 | 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] | 112 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 113 | return pci_mmio_read_config32(dev, reg); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 117 | 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] | 118 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 119 | pci_mmio_write_config8(dev, reg, value); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 123 | 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] | 124 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 125 | pci_mmio_write_config16(dev, reg, value); |
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 | 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] | 130 | { |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 131 | pci_mmio_write_config32(dev, reg, value); |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 132 | } |
Kyösti Mälkki | 2d8aff3 | 2019-01-23 16:44:55 +0200 | [diff] [blame] | 133 | |
| 134 | #endif |
| 135 | |
Kyösti Mälkki | 54d6abd | 2013-06-19 23:05:00 +0300 | [diff] [blame] | 136 | #endif /* _PCI_MMIO_CFG_H */ |