Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 2 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 3 | #ifndef PCI_OPS_H |
| 4 | #define PCI_OPS_H |
| 5 | |
| 6 | #include <stdint.h> |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 7 | #include <device/device.h> |
Kyösti Mälkki | 3e6913b | 2019-03-02 16:26:10 +0200 | [diff] [blame] | 8 | #include <device/pci_type.h> |
Elyes Haouas | 35c3ae3b | 2022-10-27 12:25:12 +0200 | [diff] [blame] | 9 | #include <arch/pci_ops.h> /* IWYU pragma: export */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 10 | |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 11 | void __noreturn pcidev_die(void); |
| 12 | |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 13 | static __always_inline pci_devfn_t pcidev_bdf(const struct device *dev) |
| 14 | { |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 15 | return (dev->path.pci.devfn << 12) | (dev->upstream->secondary << 20) | |
| 16 | (dev->upstream->segment_group << 28); |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | static __always_inline pci_devfn_t pcidev_assert(const struct device *dev) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 20 | { |
| 21 | if (!dev) |
| 22 | pcidev_die(); |
Kyösti Mälkki | 34cf561 | 2019-03-11 20:34:26 +0200 | [diff] [blame] | 23 | return pcidev_bdf(dev); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 24 | } |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 25 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 26 | #if defined(__SIMPLE_DEVICE__) |
| 27 | #define ENV_PCI_SIMPLE_DEVICE 1 |
| 28 | #else |
| 29 | #define ENV_PCI_SIMPLE_DEVICE 0 |
| 30 | #endif |
| 31 | |
| 32 | #if ENV_PCI_SIMPLE_DEVICE |
Kyösti Mälkki | 3f98d41 | 2019-07-29 16:38:14 +0300 | [diff] [blame] | 33 | |
| 34 | /* Avoid name collisions as different stages have different signature |
| 35 | * for these functions. The _s_ stands for simple, fundamental IO or |
| 36 | * MMIO variant. |
| 37 | */ |
| 38 | #define pci_read_config8 pci_s_read_config8 |
| 39 | #define pci_read_config16 pci_s_read_config16 |
| 40 | #define pci_read_config32 pci_s_read_config32 |
| 41 | #define pci_write_config8 pci_s_write_config8 |
| 42 | #define pci_write_config16 pci_s_write_config16 |
| 43 | #define pci_write_config32 pci_s_write_config32 |
| 44 | #else |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 45 | |
| 46 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 47 | u8 pci_read_config8(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 48 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 49 | return pci_s_read_config8(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 53 | u16 pci_read_config16(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 54 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 55 | return pci_s_read_config16(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 59 | u32 pci_read_config32(const struct device *dev, u16 reg) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 60 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 61 | return pci_s_read_config32(PCI_BDF(dev), reg); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 65 | void pci_write_config8(const struct device *dev, u16 reg, u8 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 66 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 67 | pci_s_write_config8(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 71 | void pci_write_config16(const struct device *dev, u16 reg, u16 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 72 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 73 | pci_s_write_config16(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static __always_inline |
Kyösti Mälkki | b603fdc | 2019-03-11 20:33:01 +0200 | [diff] [blame] | 77 | void pci_write_config32(const struct device *dev, u16 reg, u32 val) |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 78 | { |
Kyösti Mälkki | 4663f45 | 2019-03-07 14:18:28 +0200 | [diff] [blame] | 79 | pci_s_write_config32(PCI_BDF(dev), reg, val); |
Kyösti Mälkki | e079e5c | 2019-01-23 16:15:48 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 82 | #endif |
Stefan Reinauer | 43b29cf | 2009-03-06 19:11:52 +0000 | [diff] [blame] | 83 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 84 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 85 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 86 | void pci_update_config8(pci_devfn_t dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 87 | #else |
| 88 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 89 | void pci_update_config8(const struct device *dev, u16 reg, u8 mask, u8 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 90 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 91 | { |
| 92 | u8 reg8; |
| 93 | |
| 94 | reg8 = pci_read_config8(dev, reg); |
| 95 | reg8 &= mask; |
| 96 | reg8 |= or; |
| 97 | pci_write_config8(dev, reg, reg8); |
| 98 | } |
| 99 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 100 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 101 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 102 | void pci_update_config16(pci_devfn_t dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 103 | #else |
| 104 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 105 | void pci_update_config16(const struct device *dev, u16 reg, u16 mask, u16 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 106 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 107 | { |
| 108 | u16 reg16; |
| 109 | |
| 110 | reg16 = pci_read_config16(dev, reg); |
| 111 | reg16 &= mask; |
| 112 | reg16 |= or; |
| 113 | pci_write_config16(dev, reg, reg16); |
| 114 | } |
| 115 | |
Kyösti Mälkki | 9d5af5b | 2019-09-27 12:01:15 +0300 | [diff] [blame] | 116 | #if ENV_PCI_SIMPLE_DEVICE |
Aaron Durbin | 75a62e7 | 2018-09-13 02:10:45 -0600 | [diff] [blame] | 117 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 118 | void pci_update_config32(pci_devfn_t dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 119 | #else |
| 120 | static __always_inline |
Kyösti Mälkki | 5517246 | 2019-03-07 11:13:29 +0200 | [diff] [blame] | 121 | void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or) |
Elyes HAOUAS | f9e47cc | 2018-12-05 11:03:36 +0100 | [diff] [blame] | 122 | #endif |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 123 | { |
| 124 | u32 reg32; |
| 125 | |
| 126 | reg32 = pci_read_config32(dev, reg); |
| 127 | reg32 &= mask; |
| 128 | reg32 |= or; |
| 129 | pci_write_config32(dev, reg, reg32); |
| 130 | } |
| 131 | |
Angel Pons | 2f3456a | 2020-06-07 18:31:33 +0200 | [diff] [blame] | 132 | #if ENV_PCI_SIMPLE_DEVICE |
| 133 | static __always_inline |
| 134 | void pci_and_config8(pci_devfn_t dev, u16 reg, u8 andmask) |
| 135 | #else |
| 136 | static __always_inline |
| 137 | void pci_and_config8(const struct device *dev, u16 reg, u8 andmask) |
| 138 | #endif |
| 139 | { |
| 140 | pci_update_config8(dev, reg, andmask, 0); |
| 141 | } |
| 142 | |
| 143 | #if ENV_PCI_SIMPLE_DEVICE |
| 144 | static __always_inline |
| 145 | void pci_and_config16(pci_devfn_t dev, u16 reg, u16 andmask) |
| 146 | #else |
| 147 | static __always_inline |
| 148 | void pci_and_config16(const struct device *dev, u16 reg, u16 andmask) |
| 149 | #endif |
| 150 | { |
| 151 | pci_update_config16(dev, reg, andmask, 0); |
| 152 | } |
| 153 | |
| 154 | #if ENV_PCI_SIMPLE_DEVICE |
| 155 | static __always_inline |
| 156 | void pci_and_config32(pci_devfn_t dev, u16 reg, u32 andmask) |
| 157 | #else |
| 158 | static __always_inline |
| 159 | void pci_and_config32(const struct device *dev, u16 reg, u32 andmask) |
| 160 | #endif |
| 161 | { |
| 162 | pci_update_config32(dev, reg, andmask, 0); |
| 163 | } |
| 164 | |
| 165 | #if ENV_PCI_SIMPLE_DEVICE |
| 166 | static __always_inline |
| 167 | void pci_or_config8(pci_devfn_t dev, u16 reg, u8 ormask) |
| 168 | #else |
| 169 | static __always_inline |
| 170 | void pci_or_config8(const struct device *dev, u16 reg, u8 ormask) |
| 171 | #endif |
| 172 | { |
| 173 | pci_update_config8(dev, reg, 0xff, ormask); |
| 174 | } |
| 175 | |
| 176 | #if ENV_PCI_SIMPLE_DEVICE |
| 177 | static __always_inline |
| 178 | void pci_or_config16(pci_devfn_t dev, u16 reg, u16 ormask) |
| 179 | #else |
| 180 | static __always_inline |
| 181 | void pci_or_config16(const struct device *dev, u16 reg, u16 ormask) |
| 182 | #endif |
| 183 | { |
| 184 | pci_update_config16(dev, reg, 0xffff, ormask); |
| 185 | } |
| 186 | |
| 187 | #if ENV_PCI_SIMPLE_DEVICE |
| 188 | static __always_inline |
| 189 | void pci_or_config32(pci_devfn_t dev, u16 reg, u32 ormask) |
| 190 | #else |
| 191 | static __always_inline |
| 192 | void pci_or_config32(const struct device *dev, u16 reg, u32 ormask) |
| 193 | #endif |
| 194 | { |
| 195 | pci_update_config32(dev, reg, 0xffffffff, ormask); |
| 196 | } |
| 197 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 198 | u16 pci_s_find_next_capability(pci_devfn_t dev, u16 cap, u16 last); |
| 199 | u16 pci_s_find_capability(pci_devfn_t dev, u16 cap); |
| 200 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 201 | static __always_inline |
| 202 | u16 pci_find_next_capability(const struct device *dev, u16 cap, u16 last) |
| 203 | { |
| 204 | return pci_s_find_next_capability(PCI_BDF(dev), cap, last); |
| 205 | } |
| 206 | |
| 207 | static __always_inline |
| 208 | u16 pci_find_capability(const struct device *dev, u16 cap) |
| 209 | { |
| 210 | return pci_s_find_capability(PCI_BDF(dev), cap); |
| 211 | } |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 212 | |
Tim Wawrzynczak | 8a1ad13 | 2020-11-05 14:38:51 -0700 | [diff] [blame] | 213 | /* |
| 214 | * Determine if the given PCI device is the source of wake from sleep by checking PME_STATUS and |
| 215 | * PME_ENABLE bits in PM control and status register. |
| 216 | * |
| 217 | * Returns true if PCI device is wake source, false otherwise. |
| 218 | */ |
Tim Wawrzynczak | 93982c3 | 2021-04-29 09:45:59 -0600 | [diff] [blame] | 219 | bool pci_dev_is_wake_source(pci_devfn_t dev); |
Tim Wawrzynczak | 8a1ad13 | 2020-11-05 14:38:51 -0700 | [diff] [blame] | 220 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 221 | #endif /* PCI_OPS_H */ |